gpt4 book ai didi

objective-c - 如何在 Objective-C 中比较两个日期

转载 作者:IT老高 更新时间:2023-10-28 11:26:47 24 4
gpt4 key购买 nike

我有两个日期: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/

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