Right way to convert string into NSDate?(iphone)

10,129
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate* d = [df dateFromString:@"12/23/2046"];
NSLog(@"%@", d);

Result:

2046-12-23 00:00:00 +0800
Share:
10,129
B K
Author by

B K

SOreadytohelp

Updated on June 04, 2022

Comments

  • B K
    B K almost 2 years

    In my app I store the date as a string("MM/dd/YYYY" format). In the DB. later when I retrieve the string I have to compare two dates, how do I achieve this?

    If I had used the"YYYY/MM/dd" format I could have directly compared it as strings. Now I have to convert back into NSDate object using "nsdateformatter" and "dateFromString". But I get a constant date value using this no matter what I do.

    Any ideas??

  • B K
    B K over 14 years
    hmmm interesting! with"yyyyy" Its working and when I used "YYYY" it was not. Now y would that be?
  • B K
    B K over 14 years
    Thanks! If not taken care, even the smallest of the things can surprise u.