ios 如何比较目标C中的两个NSDate对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8183472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two NSDate objects in objective C
提问by Ranjit
I have objects of Date type.
我有 Date 类型的对象。
I want to compare them, and I have written an if condition in the following way:
我想比较它们,我用以下方式编写了一个 if 条件:
if (([startDate1 isEqualToDate:[self getDefaultDate]]) || (startDate1 != [ self getDefaultDate] && m_selectedDate >= m_currentDate1 && cycleStopped))
{
///execute some condition
}
Am I right or wrong in this approach?
我在这种方法中是对还是错?
One more thing. Is this way right:
还有一件事。这种方式对吗:
if (dtdate > [m_array objectatIndex:i]
{
}
Because I am getting random behavior.
因为我的行为是随机的。
回答by Luke
Here's an example using the NSDate
method, compare:
这是使用该NSDate
方法的示例,compare:
if([startDate compare: endDate] == NSOrderedDescending) // if start is later in time than end
{
// do something
}
From the class reference:
从类参考:
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.
如果...
接收者和另一个日期完全相等,NSOrderedSame。
接收者在时间上晚于 anotherDate,NSOrderedDescending。
接收者在时间上早于 anotherDate,NSOrderedAscending。
You could also use the timeIntervalSinceDate:
method if you wanted to compare two dates and find the seconds between them, for example.
例如,timeIntervalSinceDate:
如果您想比较两个日期并找到它们之间的秒数,也可以使用该方法。
EDIT:
编辑:
if(([startDate1 compare:[self getDefaultDate]] == NSOrderedSame) || ([startDate1 compare: [self getDefaultDate]] != NSOrderedSame && (([m_selectedDate compare: m_currentDate1] == NSOrderedDescending) || [m_selectedDate compare: m_currentDate1] == NSOrderedSame) && cycleStopped))
回答by Pascal
Easy way to compare NSDates (found here http://webd.fr/637-comparer-deux-nsdate):
比较 NSDates 的简单方法(在这里找到http://webd.fr/637-comparer-deux-nsdate):
#import <Foundation/Foundation.h>
@interface NSDate (Compare)
-(BOOL) isLaterThanOrEqualTo:(NSDate*)date;
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date;
-(BOOL) isLaterThan:(NSDate*)date;
-(BOOL) isEarlierThan:(NSDate*)date;
//- (BOOL)isEqualToDate:(NSDate *)date; already part of the NSDate API
@end
And the implementation:
和实施:
#import "NSDate+Compare.h"
@implementation NSDate (Compare)
-(BOOL) isLaterThanOrEqualTo:(NSDate*)date {
return !([self compare:date] == NSOrderedAscending);
}
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date {
return !([self compare:date] == NSOrderedDescending);
}
-(BOOL) isLaterThan:(NSDate*)date {
return ([self compare:date] == NSOrderedDescending);
}
-(BOOL) isEarlierThan:(NSDate*)date {
return ([self compare:date] == NSOrderedAscending);
}
@end
Simple to use:
使用简单:
if([aDateYouWant ToCompare isEarlierThanOrEqualTo:[NSDate date]]) // [NSDate date] is now
{
// do your thing ...
}
Enjoy
享受
回答by Jay Versluis
I must admit that I find the "compare" == NSWhatHaveYou business too complicated to remember (even though it's correct and it works). NSDate has two other methods that my brain finds much easier to deal with: earlierDate and laterDate.
我必须承认,我发现“比较”== NSWhatHaveYou 业务太复杂而无法记住(即使它是正确的并且有效)。NSDate 有另外两种方法,我的大脑发现它们更容易处理: earlyDate 和 laterDate。
Consider this:
考虑一下:
if ([myDate laterDate:today] == myDate) {
NSLog(@"myDate is LATER than today");
} else {
NSLog(@"myDate is EARLIER than today");
}
// likewise:
if ([myDate earlierDate:today] == myDate) {
NSLog(@"myDate is EARLIER than today");
} else {
NSLog(@"myDate is LATER than today");
}
Either method returns an NSDate object, which is either earlier or later than the other.
任一方法都返回一个 NSDate 对象,该对象早于或晚于另一个。
回答by Septronic
I really liked Luke's answer, and it worked like a charm.
我真的很喜欢卢克的回答,它很有魅力。
I used something like this:
我使用了这样的东西:
If ([enteredDate compare: [NSDate Date]]== NSOrderedSame || NSOrderedDescending)
{
//Do Something if the enteredDate is later or the same as today
}else
{
//if the enteredDate is before today's date
}
I'm not sure if that helps, but just to reiterate what Luke had written.
我不确定这是否有帮助,但只是重申卢克所写的内容。
回答by Nelson Brian
This is a simple way to check if the date you think is larger (eg. in the future) compared to the date you think is earlier. Here is what this does.
这是一种检查您认为的日期是否比您认为更早的日期更大(例如,在未来)的简单方法。这是它的作用。
- Gets the time interval (seconds and frac seconds) of both current and future date
- returns true if the date your testing is greater (time reference) the the other date
- 获取当前和未来日期的时间间隔(秒和 frac 秒)
- 如果您的测试日期大于另一个日期(时间参考),则返回 true
Here you go
干得好
- (BOOL)isDateGreaterThanDate:(NSDate*)greaterDate and:(NSDate*)lesserDate{
NSTimeInterval greaterDateInterval = [greaterDate timeIntervalSince1970];
NSTimeInterval lesserDateInterval = [lesserDate timeIntervalSince1970];
if (greaterDateInterval > lesserDateInterval)
return YES;
return NO;
}
This also works
这也有效
BOOL *isGreaterDateTruelyGreater = [greaterDate timeIntervalSince1970] > [lesserDate timeIntervalSince1970];
The way objective-c deals with dates, is less than intuitive. Basically you can use NSTimeInterval to get the number of seconds from a point in time. It usually gives a long number, for instance as i am writing this 1351026414.640865 is the time interval from 1970.
Objective-c 处理日期的方式并不直观。基本上你可以使用 NSTimeInterval 来获取某个时间点的秒数。它通常给出一个很长的数字,例如在我写这篇文章时 1351026414.640865 是 1970 年的时间间隔。
Point in time > point in time 1351026415.640865 > 1351026414.640865 The above is 1 second ahead, so it is true.
时间点 > 时间点 1351026415.640865 > 1351026414.640865 上面是提前1秒,所以是真的。
回答by cREcker
This link shows it in the best way:
此链接以最佳方式显示它:
http://www.myuiviews.com/2011/01/10/nsdate-comparison-which-is-older.html
http://www.myuiviews.com/2011/01/10/nsdate-comparison-which-is-older.html