我不明白为什么这个方法不起作用。使用 isEqualToDate: 时是否查看秒数和分秒数?
//Test that the NSDate category's DatePlusDays: method works
- (void)testNSDateCategoryDatePlusDays
{
NSInteger numberOdDatesToAdd = 1;
//Test method
NSDate *result = [NSDate datePlusDays:numberOdDatesToAdd];
//Correct result
NSDate *now = [NSDate date];
NSDate *correctResult = [now dateByAddingTimeInterval:60*60*24*numberOdDatesToAdd];
NSLog(@"correct result: %@", correctResult);
NSLog(@"result %@", result);
STAssertTrue([correctResult isEqualToDate:result], @"Dates are not equal");
}
2011-04-19 09:29:02.939 [49535:207]
正确结果:2011-04-20 08:29:02
+0000 2011-04-19 09:29:02.940 [49535:207] 结果 2011-04-20 08:29:02
+0000
+ (NSDate *)datePlusDays:(NSInteger)days
{
NSDate *now = [NSDate date];
return [now dateByAddingTimeInterval:60*60*24*days];
}
它会进行精确到微秒的比较,以查看日期是否完全相等。
如果您需要较低的精度,请使用timeIntervalSinceDate:
测试日期之间的差异是否在微秒、秒、小时、天等范围内。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)