iOS:日期比较在真实条件下失败,在模拟条件下工作

时间:2014-10-15 14:46:46

标签: ios objective-c nsdate nsdatecomponents

我正在使用此代码来比较两个日期,以查看存储日期是否基本上是昨天:

NSDate *date = [wordData objectForKey:@"date"];
NSLog(@"Word data date: %@", [wordData objectForKey:@"date"]);

NSDate *nowDate = [NSDate date];
NSLog(@"Date now: %@", nowDate);

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *date1Components = [cal components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
NSDateComponents *date2Components = [cal components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
NSComparisonResult comparison = [[cal dateFromComponents:date1Components] compare:[cal dateFromComponents:date2Components]];


if(comparison == NSOrderedAscending)
{
    NSLog(@"Word out of date, fetching new date");
    [self fetchCurrentWord];
}
if(comparison == NSOrderedDescending)
{
    NSLog(@"Word is newer");
}
else if(comparison == NSOrderedSame)
{
    NSLog(@"Same date");
}

但是,当设备上的日期发生变化时,代码正在返回NSOrderedSame,如果我通过手动更改设备时间进行模拟,则可以正常工作,或者如果我使用dateWithTimeIntervalSinceNow它可以正常工作。我添加了调试打印并且日期看起来很好,它们最初是使用NSDate创建的,所以应该没有格式差异。我对它实际上是什么感到空白。

失败日志:

Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Word is: CLIMATURE, A climate. [Obs.] Shak., 2014-10-16 23:05:16 +0000
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Word CLIMATURE, definition A climate. [Obs.] Shak.
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Word date: 2014-10-16 23:05:16 +0000
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Word data date: 2014-10-16 23:05:16 +0000
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Date now: 2014-10-17 10:25:05 +0000
Oct 17 11:25:05 Nicholas-Smiths-iPhone WOTD[2493] <Warning>: Same date

成功记录:

2014-10-15 16:35:37.748 WOTD[960:31212] Word is: CONTORTUPLICATE, Plaited lengthwise and twisted in addition, as the bud of themorning-glory. Gray., 2014-10-15 14:43:04 +0000
2014-10-15 16:35:37.749 WOTD[960:31212] Word CONTORTUPLICATE, definition Plaited lengthwise and twisted in addition, as the bud of themorning-glory. Gray.
2014-10-15 16:35:37.749 WOTD[960:31212] Word date: 2014-10-15 14:43:04 +0000
2014-10-15 16:35:37.749 WOTD[960:31212] Word data date: 2014-10-15 14:43:04 +0000
2014-10-15 16:35:37.749 WOTD[960:31212] Date now: 2014-10-15 15:35:37 +0000
2014-10-15 16:35:37.750 WOTD[960:31212] Same date

我在这个阶段手动更改了时间

2014-10-16 16:37:29.286 WOTD[1133:38254] Word is: CONTORTUPLICATE, Plaited lengthwise and twisted in addition, as the bud of themorning-glory. Gray., 2014-10-15 14:43:04 +0000
2014-10-16 16:37:29.287 WOTD[1133:38254] Word CONTORTUPLICATE, definition Plaited lengthwise and twisted in addition, as the bud of themorning-glory. Gray.
2014-10-16 16:37:29.287 WOTD[1133:38254] Word date: 2014-10-15 14:43:04 +0000
2014-10-16 16:37:29.287 WOTD[1133:38254] Word data date: 2014-10-15 14:43:04 +0000
2014-10-16 16:37:29.287 WOTD[1133:38254] Date now: 2014-10-16 15:37:29 +0000
2014-10-16 16:37:29.288 WOTD[1133:38254] Word out of date, fetching new date
2014-10-16 16:37:29.616 WOTD[1133:38538] Word PAC, definition A kind of moccasin, having the edges of the sole turned up andsewed to the upper. Knight.

1 个答案:

答案 0 :(得分:2)

您显示为失败的示例实际上是在同一天 - 这是在UTC与BST进行比较 - 在​​23:05 + 1hr的同一天,日期只是 = 00:05 =今天。

由于时区的原因,您需要注意使用日期,如果您想知道昨天发生的事情与现在相比,更好的方法可能是:

NSDate *startOfDay;
NSTimeInterval lengthOfDayInSeconds;
BOOL success = [cal rangeOfUnit:NSCalendarUnitDay startDate:&startOfDay interval:&lengthOfDayInSeconds forDate:[NSDate date]];

这为您提供了今天开始的日期startOfDay(适用于本地日历时区),然后您可以通过简单比较将其与测试日期进行比较。