Advertise here

Advertise here

Howdy, Stranger!

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

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 San FranciscoPosts: 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
  • 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,116Tutorial 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.