Correct way to convert string to NSDate? (Iphone)

In my application, I store the date as a string (format "MM / dd / YYYY"). In the DB. later, when I get the string, I need to compare two dates, how can I achieve this?

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

Any ideas?

+3
source share
1 answer
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
+17
source

Source: https://habr.com/ru/post/1730529/


All Articles