ios - 日历-isDateInToday:date 00:15今天不在吗?

标签 ios objective-c nsdate

我正在尝试比较今天是否有时间。我使用了NSCalendar [Calender isDateInToday:date]方法。但是,只是经过午夜的时间未通过测试。
时间00:14:05返回否,但21:43:47返回是。

我的测试代码:

NSCalendar *cal = [NSCalendar calendarWithIdentifier: NSCalendarIdentifierGregorian]; 
NSLog(@"date: %@, isInToday: %i, Currernt date: %@", o.deliverDate, [cal isDate:o.deliverDate inSameDayAsDate:[TimeIntervals getCurrerntLocalTime]], [TimeIntervals getCurrerntLocalTime]);

其中[TimeIntervals getCurrerntLocalTime]返回当前本地系统时间:
+(NSDate *)getCurrerntLocalTime{
NSDate* sourceDate = [NSDate date];

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
return destinationDate;
}

结果是:
date: 2015-02-09 00:14:05 +0000, isInToday: 0, Currernt date: 2015-02-09 19:07:43 +0000
date: 2015-02-09 21:43:47 +0000, isInToday: 1, Currernt date: 2015-02-09 19:07:44 +0000

有什么建议么?

最佳答案

您应该使用当前的日历:

迅速:

let isToday = NSCalendar.currentCalendar().isDateInToday(date)

关于ios - 日历-isDateInToday:date 00:15今天不在吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28423431/

相关文章:

iphone - “@public” 在 Objective-C 中是什么意思?

ios - xCode Sprite-Kit - 每个场景中的背景图像

ios - 应用程序无法从 iCloud Drive 中的文档启动

objective-c - 时间四舍五入到最接近的第 10 分钟

ios - 如何在 iOS 中显示 MBProgressHud 时添加文本?

iOS 8 - 获取当前日期为 DD/MM/YYYY

ios - iOS 中的短格式相对时间?

ios - 如何在 NSDate 中添加毫秒?

iphone - UICollectionView 中带有 Interface Builder 且没有 Storyboard 的自定义 header

ios - 将文本文件中的数据保存在数组和/或字典中