我正在尝试比较今天是否有时间。我使用了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/