gpt4 book ai didi

iOS - 将 NSDate 与另一个 NSDate 进行比较

转载 作者:行者123 更新时间:2023-11-28 21:50:02 26 4
gpt4 key购买 nike

我知道如何获取两个 NSDate 之间的差异,如下所示

NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:anyPreviousDate];

我知道它会在正秒内返回 NSTimeInterval。我想知道的是,如果我的 anyPreviousDate 大于 [NSDate date]anyPreviousDate 尚未通过,它将返回什么 future 。

只是好奇之前是否有人这样做过。

提前致谢。

最佳答案

我发现了另一种非常好的方法来做同样的事情......这是代码,我想与 stackoverflow 分享。 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];

关于iOS - 将 NSDate 与另一个 NSDate 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28671111/

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