Ios 日历-isDateInToday:date 00:15不在今天?
我想比较一下今天是否有时间。我使用了NSCalendarIos 日历-isDateInToday:date 00:15不在今天?,ios,objective-c,nsdate,Ios,Objective C,Nsdate,我想比较一下今天是否有时间。我使用了NSCalendar[calendar isDateInToday:date]方法。然而,刚刚过了午夜的一段时间却没有通过测试。 时间00:14:05返回否,但21:43:47返回是 我的测试代码: NSCalendar *cal = [NSCalendar calendarWithIdentifier: NSCalendarIdentifierGregorian]; NSLog(@"date: %@, isInToday: %i, Currernt dat
[calendar 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]);
其中[时间间隔GetCurrentLocaltime]
返回当前本地系统时间:
+(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)
您应该使用当前日历: 迅速:
let isToday = NSCalendar.currentCalendar().isDateInToday(date)
不知何故,你会被时区所影响,但很难说是怎么回事,因为你没有向我们展示你是如何设置的。@HotLicks谢谢你的回复。我已经更新了
[TimeInterval GetCurrentLocaltime]
实现。然而,我已经打印出了我正在比较的两个时间,很明显2015-02-09 00:14:05应该在2015-02-09 19:07:43的同一天,为什么对于当前本地时间,您不简单地执行[NSDate date]
?(您不理解NSdIT如何处理时区。我建议您研究一下。请考虑NSCANDEAR正在解释本地时区中的时间值。)给我UTC时间。我使用的方法是StackOverflow的解决方案。但我的问题是,isDate:date inSameDayAsDate:otherDate
方法在00:14:05和19:07:43时返回NO,不知怎的,你会被时区占用,但很难说怎么做,因为你没有告诉我们你是如何设置的。@HotLicks谢谢你的回答。我很高兴我已经更新了[TimeInterval GetCurrentLocaltime]
实现。但是我已经打印出了我正在比较的两个时间,很明显2015-02-09 00:14:05应该在2015-02-09 19:07:43的同一天。为什么对于当前的本地时间,您不只是执行[NSDate date]
?(您不理解NSdIT如何处理时区。我建议您研究一下。请考虑NSCANDEAR正在解释本地时区中的时间值。)给我UTC时间。我使用的方法是StackOverflow的解决方案。但我的问题是,isDate:date inSameDayAsDate:otherDate
方法返回00:14:05和19:07:43的否