如何在iOS 8.1中转换“Tue,14 Jul 2015 15:00:00 +0000”

时间:2015-07-15 01:59:24

标签: ios nsdateformatter

我尝试通过这种方式将字符串转换为NSDate

NSString *str = @"Tue, 14 Jul 2015 15:00:00 +0000";

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss zzzz"];

NSDate* aDate = [formatter dateFromString:str];

[formatter setDateFormat:@"MMM dd,yyyy HH:mm:ss"];

NSLog(@"%@", [formatter stringFromDate:aDate]);

但它是空的,真正的方法是什么?

1 个答案:

答案 0 :(得分:1)

我试过这个,结果应该是:

Jul 14,2015 16:00:00 (based on my time zone)

以下是代码:

NSString *str = @"Tue, 14 Jul 2015 15:00:00 +0000";

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];


[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];

NSDate* aDate = [formatter dateFromString:str];

[formatter setDateFormat:@"MMM dd,yyyy HH:mm:ss"];
//formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; if you want to avoid converting the time to the current timezone

NSLog(@"%@", [formatter stringFromDate:aDate]);

Date Format Patterns