简体   繁体   English

如何比较目标C中的两个NSDate对象

[英]How to compare two NSDate objects in objective C

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. 因为我得到随机行为。

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. 接收者和另一个日子完全相同,NSOrderedSame。

The receiver is later in time than anotherDate, NSOrderedDescending. 接收器的时间晚于anotherDate,NSOrderedDescending。

The receiver is earlier in time than anotherDate, NSOrderedAscending. 接收器的时间早于另一个日期,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))

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 请享用

I must admit that I find the "compare" == NSWhatHaveYou business too complicated to remember (even though it's correct and it works). 我必须承认,我发现“比较”== NSWhatHaveYou业务太复杂而无法记住(即使它是正确的并且有效)。 NSDate has two other methods that my brain finds much easier to deal with: earlierDate and laterDate. 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对象,该对象比另一个更早或更晚。

http://pinkstone.co.uk/how-to-compare-two-nsdates/ http://pinkstone.co.uk/how-to-compare-two-nsdates/

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. 我不确定这是否有帮助,但只是重申卢克所写的内容。

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. 这就是它的作用。

  1. Gets the time interval (seconds and frac seconds) of both current and future date 获取当前和未来日期的时间间隔(秒和frac秒)
  2. returns true if the date your testing is greater (time reference) the the other date 如果测试日期更长(时间参考),则返回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. objective-c处理日期的方式不太直观。 Basically you can use NSTimeInterval to get the number of seconds from a point in time. 基本上,您可以使用NSTimeInterval来获取某个时间点的秒数。 It usually gives a long number, for instance as i am writing this 1351026414.640865 is the time interval from 1970. 它通常给出一个很长的数字,例如我正在写这个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秒,所以这是真的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM