Search code examples
iphoneobjective-ccocoa-touchios4

How to compare two dates in iphone?


I want to compare the todays date and other date coming from my database. For example, 2011-03-25 compare it with 2011-02-25

How can i formate my database date as the NSDateFormatter require NSDdate datatype ?


Solution

  • 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.
    

    for more read the SO post

    Just go through the blog post, there are many date related utility functions.

    http://arstechnica.com/apple/guides/2010/03/how-to-real-world-dates-with-the-iphone-sdk.ars/2