gpt4 book ai didi

objective-c - iOS:比较两个日期

转载 作者:IT老高 更新时间:2023-10-28 12:22:03 26 4
gpt4 key购买 nike

我有一个 NSDate 必须与其他两个 NSDate 进行比较,我尝试使用 NSOrderAscendingNSOrderDescending但是如果我的日期与其他两个日期相同?

示例:如果我有一个 myDate = 24/05/2011 和另外两个是一个 = 24/05/2011 和两个 24/05/2011 我可以用什么?

最佳答案

根据Apple documentation of NSDate compare:

Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.

- (NSComparisonResult)compare:(NSDate *)anotherDate

Parameters anotherDate

The date with which to compare the receiver. This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of Mac OS X.

Return Value

If:

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

换句话说:

if ([date1 compare:date2] == NSOrderedSame) ...

请注意,在您的特定情况下,阅读和编​​写此内容可能更容易:

if ([date2 isEqualToDate:date2]) ...

Apple Documentation about this one .

关于objective-c - iOS:比较两个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6112075/

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