我尝试通过这种方式将字符串转换为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]);
但它是空的,真正的方法是什么?
答案 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]);