日历-isDateInToday:日期00:15不在今天?

时间:2015-02-10 03:11:59

标签: ios objective-c nsdate

我想比较今天是否有时间。我使用了NSCalendar [Calender isDateInToday:date]方法。然而,刚刚过了午夜的时间未通过测试。 时间00:14:05返回NO但21:43:47返回YES。

我的测试代码:

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

有什么建议吗?

1 个答案:

答案 0 :(得分:6)

你应该拿走你当前的日历:

在swift:

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