Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two NSDate objects in objective C

Tags:

ios

nsdate

I have objects of Date type.

I want to compare them, and I have written an if condition in the following way:

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.

like image 833
Ranjit Avatar asked Nov 18 '11 13:11

Ranjit


People also ask

What is NSDate object?

NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).


3 Answers

Here's an example using the NSDate method, 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.

You could also use the timeIntervalSinceDate: method if you wanted to compare two dates and find the seconds between them, for example.

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))
like image 180
Luke Avatar answered Oct 18 '22 16:10

Luke


Easy way to compare NSDates (found here 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

like image 40
Pascal Avatar answered Oct 18 '22 16:10

Pascal


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.

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.

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

like image 7
Jay Versluis Avatar answered Oct 18 '22 15:10

Jay Versluis