0

Possible Duplicates:
Comparing dates
How to compare two dates in Objective-C

i want to compare 2 strings who have a date format "11-06-2011" and "12-06-2011" for exemple. i want to see if the second date is greater than the first or not can you help me please ?


i tried this code

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MM-YYYY"];
    NSDate *date1 = [dateFormat dateFromString:deparatureDateField.text];
    NSDate *date2 = [dateFormat dateFromString:comebackDateField.text];
    [dateFormat release];
    NSTimeInterval timeInterval = [date1 timeIntervalSinceDate:date2];
    if(timeInterval >= 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"come back date should be greater" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
        [alert show];
    }

but i alwayse have an alert, NSTimeInterval timeInterval is alwayse NULL

Community
  • 1
  • 1
user794317
  • 45
  • 1
  • 6

3 Answers3

5

To find out if one date is larger than the other you'd have to do this:

NSString *string1 = @"11-06-2011";
NSString *string2 = @"12-06-2011";

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
NSDate *date1 = [dateFormat dateFromString:string1];
NSDate *date2 = [dateFormat dateFromString:string2];
[dateFormat release];

NSComparisonResult comparisonResult = [date1 compare:date2];

/*
NSComparisonResult can be either of these values:
 - NSOrderedAscending (-1) //if date1 < date2
 - NSOrderedSame (0)       //if date1 = date2
 - NSOrderedDescending (1) //if date1 > date2
*/

Alternatively (to compare:) you could use timeIntervalSinceDate: to get the actual time difference:

NSTimeInterval timeInterval = [date1 timeIntervalSinceDate:date2];
//timeInterval will be
// - nagative if ascending,
// - zero if same,
// - positive if descending
Regexident
  • 29,441
  • 10
  • 93
  • 100
  • @Regexident @murat thakyou but i think my question are not clear. I don't want to see if there are equal, i want to see if the second date is greater than the first or not – user794317 Jun 11 '11 at 22:36
  • @user794317: updated my answer. See second snippet. – Regexident Jun 11 '11 at 22:44
  • @Regexident thank you very much , but i alwayse have a trouble , i updated my question – user794317 Jun 11 '11 at 23:33
  • @user794317: My bad. I got the date format string wrong. The `YYYY` got to be smallcase, `dd-MM-yyyy`, that is. – Regexident Jun 12 '11 at 00:07
  • @user794317: I'm always mixing up the cases with those date formats. :P Btw, with a click on the green checkmark right beneath the vote you can accept the answer and mark the question as solved. – Regexident Jun 12 '11 at 00:26
1

Actually it depends what you will do after comparison. If you want to keep date info as a string and compare the strings is

   [@"11-06-2011" isEqualToString: @"12-06-2011"]

is enough for you

murat hacioglu
  • 166
  • 2
  • 10
0

look at NSDates timeIntervalSinceNow, you can compare the time interval from both dates to see which one is the most recent etc.

NSTimeInterval *interval = [someDate timeIntervalSinceNow];
Nik Burns
  • 3,363
  • 2
  • 29
  • 37