[英]iPhone SDK NSString To NSDate
I got a string from parsing a XML file which looks like this: Fri, 09 Apr 2010 00:00:45 +0200 and the corresponding pattern should be this "EEE, dd MMM yyyy HH:mm:ss ZZ", but I get (null). 我从解析XML文件得到一个字符串,如下所示:星期五,2010年4月9日00:00:45 +0200,相应的模式应该是“EEE,dd MMM yyyy HH:mm:ss ZZ”,但我得到了(空值)。
This is my code: 这是我的代码:
NSString *dateString = @"Fri, 09 Apr 2010 00:00:45 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"date:%@",date); // result date:(null)
Edit: 编辑:
This works for me now, I had to switch to en-US locale: 这对我有用,我不得不切换到en-US语言环境:
NSLocale* usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];
NSDate *date = [dateFormatter dateFromString:dateString];
Your code worked for me when I changed this line: 当我改变这一行时,你的代码对我有用:
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];
to this line: 到这一行:
[dateFormatter setDateFormat:@"EEE',' dd MMM yyyy HH:mm:ss ZZ"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.