如何在Objective-C中比较两个日期 [英] How to compare two dates in Objective-C
本文介绍了如何在Objective-C中比较两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个日期:2009-05-11
和当前日期.我想检查给定的日期是否是当前日期.这怎么可能.
I have two dates: 2009-05-11
and the current date. I want to check whether the given date is the current date or not. How is this possible.
推荐答案
在 NSDate 中
– isEqualToDate:
– earlierDate:
– laterDate:
– compare:
当你使用 - (NSComparisonResult)compare:(NSDate *)anotherDate
时,你会得到以下之一:
When you use - (NSComparisonResult)compare:(NSDate *)anotherDate
,you get back one of these:
The receiver and anotherDate are exactly equal to each other, NSOrderedSame
The receiver is later in time than anotherDate, NSOrderedDescending
The 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 %@", mile, now); break;
case NSOrderedSame: NSLog(@"%@ is the same as %@", mile, now); break;
default: NSLog(@"erorr dates %@, %@", mile, now); break;
}
[mile release];
这篇关于如何在Objective-C中比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文