ios - 如何仅获取日期并与 iOS 中的两个日期进行比较?

标签 ios objective-c date

如何在 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/

相关文章:

ios - fatal error : NSArray element failed to match the Swift Array Element type

iphone - 阻止 Iphone 应用程序的 3G 连接

date - 昨天日期使用 VBS 代码和批处理文件(格式 : YYYY_MM_DD)

r - 如何在R中查找最近一周

java - 新的 cassandra 绑定(bind)语句 getDate 方法

ios - 禁用约束 Storyboard

ios - CALayer - 如何创建具有纯色背景和透明文本的图像?

python - Apple 会允许 iOS App Store 中使用 Python 中的 Kivi 框架吗?

objective-c - 委托(delegate)方法似乎是@required,它被声明为@optional

ios - 如果我有两种不同类型的单元格,我该如何使用 FetchedResultsController?