-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
  • 2,036
  • 6
  • 33
  • 48
Navi
  • 1,696
  • 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
  • 4
    If you think a question is a duplicate, please mark it is a duplicate rather than answering with a link to the other question and answer. – Abizern Jun 04 '13 at 10:17
  • Okay but i don't see any option to mark duplicate. – Divyu Jun 04 '13 at 10:19
  • 1
    It's under the link for closing. It gives you options for why you are recommending closure. One of the reasons is that it is a duplicate. You can paste the link to the duplicate in there. – Abizern Jun 04 '13 at 10:21
  • @user2439088: Give respect to others.... Abizern helps you – Anoop Vaidya Jun 04 '13 at 10:40
  • @AnoopVaidya i respect you , Abizern sorry for that comment – Navi Jun 04 '13 at 10:42
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