gpt4 book ai didi

iphone - NSDate isEqualToDate : not working - does it look at seconds and split seconds?

转载 作者:搜寻专家 更新时间:2023-10-30 19:55:36 28 4
gpt4 key购买 nike

我不明白为什么这个方法不起作用。使用 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] 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];
}

最佳答案

它比较到微秒以查看日期是否完全相等。

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

关于iphone - NSDate isEqualToDate : not working - does it look at seconds and split seconds?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5713638/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com