NSDate isEqualToDate:不工作 - 看看秒和分秒? [英] NSDate isEqualToDate: not working - does it look at seconds and split seconds?

查看:118
本文介绍了NSDate isEqualToDate:不工作 - 看看秒和分秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解为什么这种方法不起作用。使用isEqualToDate时是否查看秒和分秒:?

I cannot understand why this method is not working. Does it look at the seconds and split seconds when using 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

2011-04-19 09:29:02.939 [49535:207] correct result: 2011-04-20 08:29:02 +0000 2011-04-19 09:29:02.940 [49535:207] result 2011-04-20 08:29:02 +0000



+ (NSDate *)datePlusDays:(NSInteger)days
{
    NSDate *now = [NSDate date];
    return [now dateByAddingTimeInterval:60*60*24*days];
}


推荐答案

它比较微秒看看日期是否完全相同。

It compares down to the microsecond to see if the dates are exactly equal.

如果您需要较低的精度,请使用 timeIntervalSinceDate:来测试日期在微秒,秒范围内,小时,天等彼此。

If you need less precision, use timeIntervalSinceDate: to test for the dates being within a range of microseconds, seconds, hours, days etc, of each other.

这篇关于NSDate isEqualToDate:不工作 - 看看秒和分秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆