作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有两个日期: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, 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 - 如何在 Objective-C 中比较两个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/949416/
我是一名优秀的程序员,十分优秀!