Convert NSString to NSDate with string format “Sun, 08 Jan 2012 13:57:38 +0000”

送分小仙女□ 提交于 2019-12-23 12:16:23

问题


I actually retrieve string date with the format "Sun, 08 Jan 2012 13:57:38 +0000" For that I use the dateFormat "EEE, dd MMM yyyy HH:mm:ss ZZZ" But it doesn't 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 is (null)

I don't understand where I am wrong. Please tell me if you see something wrong.

Thanks


回答1:


Your date string is in English. If your device or simulator locale is set to another language, that could be the problem. If so, you need to set the locale for the NSDateFormatter to English.

Like this:

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


来源:https://stackoverflow.com/questions/8778256/convert-nsstring-to-nsdate-with-string-format-sun-08-jan-2012-135738-0000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!