Harshal
Harshal

Reputation: 201

how to compare dates in objective c

I want compare two dates one would be the current date and other date stored in database. So I want to find out which date is grater from both.

Upvotes: 2

Views: 3416

Answers (3)

Robin
Robin

Reputation: 10011

You can use compare: method to find which date is earlier or later check the documentation on this

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/

Edit:

NSDateFormatter * formatter= [NSDateFormatter alloc] init];
NSDate *date = [formatter dateFromString:stringDate];
[formatter release];

then compare them.

Upvotes: 0

mmccomb
mmccomb

Reputation: 13807

You can use the compare method of the NSDate class

[myDate compare:someOtherDate]

This will return one of three values of type NSComparisonResult...

  • NSOrderedSame if both dates are equal
  • NSOrderedDescending if myDate comes after someOtherDate
  • NSOrderedAscending if myDate comes before someOtherDate

Upvotes: 4

KingofBliss
KingofBliss

Reputation: 15115

Check apple documentation.

You can use the following methods,

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

Upvotes: 3

Related Questions