如何比较Objective-C中的两个日期

如何比较Objective-C中的两个日期,第1张

概述我有两个日期:2009-05-11和当前日期。我想检查给定的日期是否是当前日期。这怎么可能。 Cocoa has couple of methods for this: 在NSDate – isEqualToDate: – earlierDate: – laterDate: – compare: 当你使用 – (NSComparisonResult)compare:(NSDate *) 我有两个日期:2009-05-11和当前日期。我想检查给定的日期是否是当前日期。这怎么可能。解决方法 Cocoa has couple of methods for this:

在NSDate

– isEqualToDate:  – earlIErDate:  – laterDate:  – compare:

当你使用 – (NSComparisonResult)compare:(NSDate *)anotherDate,你回到其中之一:

The receiver and anotherDate are exactly equal to each other,NSOrderedSameThe receiver is later in time than anotherDate,NSOrderedDescendingThe receiver is earlIEr in time than anotherDate,NSOrderedAscending.

例:

NSDate * Now = [NSDate date];NSDate * mile = [[NSDate alloc] initWithString:@"2001-03-24 10:45:32 +0600"];NSComparisonResult result = [Now compare:mile];NSLog(@"%@",Now);NSLog(@"%@",mile);switch (result){    case NSOrderedAscending: NSLog(@"%@ is in future from %@",mile,Now); break;    case NSOrderedDescending: NSLog(@"%@ is in past from %@",Now); break;    case NSOrderedSame: NSLog(@"%@ is the same as %@",Now); break;    default: NSLog(@"erorr dates %@,%@",Now); break;}[mile release];
总结

以上是内存溢出为你收集整理的如何比较Objective-C中的两个日期全部内容,希望文章能够帮你解决如何比较Objective-C中的两个日期所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1047822.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存