Convert NSString to NSDate with string format "Sun, Jan 08, 2012 1:57:38 p.m."

I really get a lowercase date in the format "Sun, 08 Jan 2012 13:57:38 +0000" For this I use dateFormat "EEE, dd MMM yyyy HH: mm: ss ZZZ" But this does not work ...

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZZ"]; NSLog(@"%@",[dict objectForKey:@"created_at"]); NSDate *dateFromString = [dateFormatter dateFromString:[dict objectForKey:@"created_at"]]; NSLog(@"%@",dateFromString); 

For the first NSLog it displays me "Sun, 08 Jan 2012 13:57:38 +0000" But the second - (null)

I do not understand where I am mistaken. Please tell me if you see something wrong.

thanks

+4
source share
1 answer

The date string is written in English. If a different language is installed for your device or simulator language, this may be a problem. If so, you need to set the locale for NSDateFormatter to English.

Like this:

 [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]]; 
+3
source

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


All Articles