【发布时间】:2014-02-23 20:35:03
【问题描述】:
我正在尝试比较 2 个日期,但我少了 1 天
这是一个sn-p:
NSDateComponents* dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear: 2014];
[dateComponents setMonth: 1];
[dateComponents setDay: 31];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDate* otherDay = [calendar dateFromComponents: dateComponents];
NSDate * todaydate= [NSDate date];
if ([otherDay compare:todaydate]>= NSOrderedDescending)
{
NSLog(@"In If other Date= %@ & Today = %@ ", otherDay,todaydate);
}else
{
NSLog(@"I am in else other date= %@ and today = %@ ",otherDay, todaydate);
}
我得到的日志是:
I am in else other date= 2014-01-30 18:30:00 +0000 and today = 2014-01-31 08:34:21 +0000
为什么显示其他date = 30th jan 2014?
【问题讨论】:
-
读起来像一个off-by-1错误,但不熟悉iOS框架的那个方面,我只能暗示解决方案。尝试使用 earlyDate:、isEqual: 和 laterDate: 而不是 compare: 看看问题是否仍然存在
-
我想问题是 GMT 偏移量
标签: ios iphone objective-c nsdate nsdatecomponents