-3

I am trying to compare two dates whether which one is the new or old, my dates are in following format:

2013-06-03 09:30:35

(YYYY-MM-DD HH:MM:SS)

Please give me some ideas, both are currently in string format?

So is that possible to convert ad check?

Sanjay Joshi
  • 1,847
  • 6
  • 31
  • 46
Navi
  • 1,656
  • 2
  • 17
  • 36

2 Answers2

2

Convert that strings into NSDate using dateFormatter. You can check this link Here is good discussion of your problem How to compare two NSDates: Which is more recent?

Community
  • 1
  • 1
Divyu
  • 1,325
  • 9
  • 21
1

try this

NSString to NSDate

NSString *dateString = @"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

NSDate convert to NSString:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
[dateFormatter release];

Please read this

enter link description here

if ([yourFirstDate compare:secondDate] == NSOrderedDescending) {
    NSLog(@"yourFirstDate is later than secondDate");        

} else if ([yourFirstDate compare:secondDate] == NSOrderedAscending) {
    NSLog(@"yourFirstDate is earlier than secondDate");

} else {
    NSLog(@"dates are the same");

}

same as @DIVYU 's Link