[英]Calendar -isDateInToday:date 00:15 not in today?
我正在嘗試比較今天是否有時間。 我使用了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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.