如何格式化此日期

时间:2011-06-09 15:48:57

标签: iphone nsdateformatter

嗨我有一个像这样的字符串:Thu, 09 Jun 2011 5:00 pm CST,其中CST可以是任何时区或其他东西。

帮助我已经尝试过了,但它是空的:

NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter2 setDateFormat:@"EEE, dd MMM yyyy h:mm z"];
[formatter2 setLocale:[[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]autorelease]];
[formatter2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDate *time = [formatter2 dateFromString:@"Thu, 09 Jun 2011 5:00 pm KST"];
[formatter2 release];
NSLog(@"%@",time); //null

我做错了吗?帮助我,我是新人。

1 个答案:

答案 0 :(得分:3)

您不是在考虑AM / PM。我相信那是'a'

尝试:

NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter2 setDateFormat:@"EEE, dd MMM yyyy h:mm a z"];
[formatter2 setLocale:[[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]autorelease]];
[formatter2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDate *time = [formatter2 dateFromString:@"Thu, 09 Jun 2011 5:00 pm KST"];
[formatter2 release];
NSLog(@"%@",time); //null