Take the 2-minute tour ×

I have two dates: 2009-05-11 and the current date. I want to check whether the given date is the current date or not. How is this possible.

share|improve this question
add comment

11 Answers

Cocoa has couple of methods for this:

in NSDate

– isEqualToDate:  
– earlierDate:  
– laterDate:  
– compare:

When you use - (NSComparisonResult)compare:(NSDate *)anotherDate ,you get back one of these:

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];
share|improve this answer
How does this answer the original question of "whether the given date is the current date or not"? [NSDate date] will be NSOrderedSame to any fixed date not for 24 hours but for less than a second. –  Chei Jul 14 '11 at 14:06
This does not work for iOS. See accepted answer on: stackoverflow.com/questions/14702626/… –  Alexander Longbeach Apr 28 '13 at 4:02
@HappyFlow the question is tagged cocoa it does not mention iOS, for more info try google "cocoa" vs "cocoa touch" –  stefanB Apr 29 '13 at 4:52
@stefanB My bad, I didn't see that. –  Alexander Longbeach Apr 29 '13 at 9:55
add comment
NSDate *today = [NSDate date]; // it will give you current date
NSDate *newDate = [NSDate dateWithString:@"xxxxxx"]; // your date 

NSComparisonResult result; 
//has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending

result = [today compare:newDate]; // comparing two dates

    NSLog(@"today is less");
else if(result==NSOrderedDescending)
    NSLog(@"newDate is less");
    NSLog(@"Both dates are same");

There are other ways that you may use to compare an NSDate objects. Each of the methods will be more efficient at certain tasks. I have chosen the compare method because it will handle most of your basic date comparison needs.

share|improve this answer
add comment

Here buddy. This function will match your date with any specified date and will tell weather they match or not. You can also modify the components to match your requirements.

- (BOOL)isSameDay:(NSDate*)date1 otherDay:(NSDate*)date2 {
NSCalendar* calendar = [NSCalendar currentCalendar];

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];

return [comp1 day]   == [comp2 day] &&
[comp1 month] == [comp2 month] &&
[comp1 year]  == [comp2 year];}

Regards, Naveed Butt

share|improve this answer
This is the correct answer. While the top answer may be more general, this provides a solution specific to the problem presented. –  elsurudo Apr 29 '12 at 11:51
Great answer.Thanks –  moujib Mar 11 '13 at 15:14
add comment

This category (found here http://webd.fr/637-comparer-deux-nsdate) offers a neat way to compare NSDates:

#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


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);


Simple to use:

if([aDateYouWantToCompare isEarlierThanOrEqualTo:[NSDate date]]) // [NSDate date] is now
    // do your thing ...
share|improve this answer
thank you! This is a lot better than having code with the NSComparisonResults -- incomprehensible for dates -- directly in it. –  Yar Feb 27 at 22:18
add comment

If you make both dates NSDates you can use NSDate's compare: method:

NSComparisonResult result = [Date2 compare:Date1];

    NSLog(@"Date1 is in the future");
else if(result==NSOrderedDescending)
    NSLog(@"Date1 is in the past")
    NSLog(@"Both dates are the same");

You can take a look at the docs here.

share|improve this answer
add comment

The best way I found was to check the difference between the given date and today:

NSCalendar* calendar = [NSCalendar currentCalendar];
NSDate* now = [NSDate date];
int differenceInDays =
    [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:date] -
    [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:now];

According to Listing 13 of Calendrical Calculations in Apple's Date and Time Programming Guide [NSCalendar ordinalityOfUnit:NSDayCalendarUnit inUnit: NSEraCalendarUnit forDate:myDate] gives you the number of midnights since the start of the era. This way it's easy to check whether the date is yesterday, today, or tomorrow.

switch (differenceInDays) {
    case -1:
        dayString = @"Yesterday";
    case 0:
        dayString = @"Today";
    case 1:
        dayString = @"Tomorrow";
    default: {
        NSDateFormatter* dayFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dayFormatter setLocale:usLocale];
        [dayFormatter setDateFormat:@"dd MMM"];
        dayString = [dayFormatter stringFromDate: date];
share|improve this answer
add comment

By this method also you can compare two dates

NSDate * dateOne = [NSDate date];
NSDate * dateTwo = [NSDate date];

if([dateOne compare:dateTwo] == NSOrderedAscending)

share|improve this answer
Interesting constants to compare with might be: NSOrderedAscending, NSOrderedSame and NSOrderedDescending, but the OP probably wants NSOrderedSame for his particular problem. –  Kris Aug 2 '13 at 12:27
add comment
NSDateFormatter *df= [[NSDateFormatter alloc] init];

[df setDateFormat:@"yyyy-MM-dd"];

NSDate *dt1 = [[NSDate alloc] init];

NSDate *dt2 = [[NSDate alloc] init];

dt1=[df dateFromString:@"2011-02-25"];

dt2=[df dateFromString:@"2011-03-25"];

NSComparisonResult result = [dt1 compare:dt2];

switch (result)

        case NSOrderedAscending: NSLog(@"%@ is greater than %@", dt2, dt1); break;

        case NSOrderedDescending: NSLog(@"%@ is less %@", dt2, dt1); break;

        case NSOrderedSame: NSLog(@"%@ is equal to %@", dt2, dt1); break;

        default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;


Enjoy coding......

share|improve this answer
add comment

What you really need is to compare two objects of the same kind.

  1. Create an NSDate out of your string date (@"2009-05-11") :

  2. If the current date is a string too, make it an NSDate. If its already an NSDate, leave it.

share|improve this answer
You can't compare two objects, by using == –  oxigen Jun 4 '09 at 10:31
my mistake, answered another question at the same time. Thank you for commenting that. –  David Salzer Jun 4 '09 at 11:43
add comment


NSString *date = @"2009-05-11"
NSString *nowDate = [[[NSDate date]description]substringToIndex: 10];
if([date isEqualToString: nowDate])
// your code
share|improve this answer
I think it'd be much more useful to turn "date" into an NSDate object and then compare NSDate objects. This code will work, but substringToIndex:10 is really a hack. If you need more information, such as which date is more recent - you'd need to do it that way, too. –  Ben Gotow Jun 4 '09 at 19:54
Of course, you can compare NSDate objects. But NSDate has information about Date and TIME Your need 1. Create NSDate from string 2. REmove from current-date information about time (set time to 00:00:00) And only after that compare dates. I think that this way is not easy and quick. –  oxigen Jun 5 '09 at 9:28
If you're only interested in the day itself, you can create NSDates then use NSCalendar to get NSDateComponents out of it, at which point you can numerically compare just the values you're interested in. You could also get components for the number of days since the epoch, to use in a single comparison. –  Jim Dovey Jun 5 '09 at 12:46
You shouldn't compare by substring'ing a NSDate description because Apple can change the format and then this wouldn't work anymore. I tell this because thats the same reason why my App has started crashing on new OS update. –  relikd Mar 30 '11 at 14:41
add comment

Your Answer


