简体   繁体   中英

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.

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

Check apple documentation .

You can use the following methods,

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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