我正在比较两个称为serverDate
和的NSDate localDate
。尽管两个日期输出的时间都相同,但我仍认为serverDate
它比的要晚localDate
。
这是我的比较代码:
if ([serverDate timeIntervalSinceReferenceDate] > [localDate timeIntervalSinceReferenceDate]) {
NSLog(@"server date more recent");
} else if ([serverDate timeIntervalSinceReferenceDate] <= [localDate timeIntervalSinceReferenceDate]){
NSLog(@"local date more recent");
}
我已经非常精确NSDateFormatter
地将日期显示为"yyyy-MM-dd HH:mm:ss:SSSSSSSSSSZ"
,它输出以下内容:Server date: 2014-09-17 23:20:02:5090000000-0700, local date: 2014-09-17 23:20:02:5090000000-0700
。但是即使那样,serverDate
仍被视为最近的日期。
我做错了什么?感谢您的输入。
您不应将其NSimeIntervell
用作NSDate
具有Compare
日期比较compare
功能的函数
if ([serverDate compare:localDate ] == NSOrderedDescending) {
NSLog(@"serverDate is later than localDate");
} else if ([serverDate compare:localDate] == NSOrderedAscending) {
NSLog(@"serverDate is earlier than localDate");
} else {
NSLog(@"dates are the same");
}
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句