如何在 iOS 中只获取日期并与两个日期进行比较? 实际上我有来自服务器的日期
例如:2015-9-23
_offerEnd.text = [_offers valueForKeyPath:@"offer_valid_upto"]
此日期与当前日期进行比较。 我已经尝试使用它来获取当前日期。
NSDate *today = [NSDate date];
NSLog(@" total date %@",today);
unsigned int flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:flags fromDate:today];
NSDate* dateOnly = [calendar dateFromComponents:components];
NSLog(@" the date only o/p :%@",dateOnly);
但 o/p 正在变得不同
the date only o/p :2015-10-12 18:30:00 +0000
请帮我做这个和比较
提前致谢
最佳答案
这里的 strDate1 是来自您的服务器的日期。
试试这个:
NSString *strDate1=@"2015-09-13";
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date1=[dateFormatter dateFromString:strDate1];
NSDate *today = [NSDate date];
NSLog(@" today date %@",today);
NSString *strDate2=[dateFormatter stringFromDate:today];
NSLog(@"date 1 : %@ and date2: %@",strDate1,strDate2);
if ([strDate1 isEqualToString:strDate2])
{
NSLog(@"Equal");
}
else
{
NSLog(@"Not Equal");
}
NSDate *date2=[dateFormatter dateFromString:strDate2];
NSComparisonResult result = [date2 compare:date1];
switch (result) {
case NSOrderedAscending:
NSLog(@"Ascending");
break;
case NSOrderedDescending:
NSLog(@"Descending");
break;
case NSOrderedSame:
NSLog(@"Same");
break;
default:
break;
}
关于ios - 如何仅获取日期并与 iOS 中的两个日期进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33099706/