how to compare 2 dates

jenesappjenesapp Posts: 141Registered Users
edited November 2010 in iOS SDK Development
i have 2 dates
date1 = '02-04-2010';
date2 = '09-05 -2010';

in this how to compare which one greater
pls help me
Post edited by jenesapp on


  • baja_yubaja_yu Posts: 6,215Moderators, Registered Users Admin
    edited November 2010
    Try this
    - (NSInteger) daysBetweenStartDate:(NSDate*) startDate andEndDate:(NSDate*) endDate {
    	NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    	NSDateComponents *dateComponents = [gregorian components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0]; 
  • dany_devdany_dev Posts: 4,696Tutorial Authors, Registered Users @ @ @ @ @
    edited November 2010
    some usefull tips using dates
    How-to: real-world dates with the iPhone SDK
  • smithdale87smithdale87 Posts: 4,447iPhone Dev SDK Supporter @ @ @ @ @
    edited November 2010
    doesnt NSDate have its own compare methods?
    - (NSComparisonResult)compare:(NSDate *)anotherDate
  • Duncan CDuncan C Posts: 9,098Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited November 2010
    doesnt NSDate have its own compare methods?
    - (NSComparisonResult)compare:(NSDate *)anotherDate

    smithdale87 has it right.

    If you are comparing 2 NSDate objects, and not two strings that CONTAIN dates, you can use the compare method for NSDates directly.

    If you want to compare two strings that contain dates you would need to first convert them to NSDates using an NSDateFormatter object, and then compare the resulting NSDates.
    Duncan C

    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
Sign In or Register to comment.

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!