将美国时间和日期转换为GMT

时间:2014-03-08 20:13:43

标签: ios iphone objective-c

我正在尝试将日期/时间从美国转换为GMT。它似乎不起作用。它一直返回null。

int subObjects = ((NSArray *)jsonResult[@"match"]).count;
for (int i = 0; i <= subObjects-1; i++) {


    NSString *date = [NSString stringWithFormat:@"%@ %@",[[[jsonResult valueForKey:@"match"] valueForKey:@"playdate"] objectAtIndex:i], [[[jsonResult valueForKey:@"match"] valueForKey:@"time"] objectAtIndex:i]];


    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setTimeZone: [NSTimeZone timeZoneWithName:@"US/Arizona"]];
    [df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    [df setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
    NSDate *myDate = [df dateFromString:[NSString stringWithFormat:@"%@", date]];

}

myDate返回(null)。

我已经检查了我的日期字符串并且它返回了这个,所以这不是问题所在。我搜索了stackoverflow,这就是我所做的。我究竟做错了什么?

日期记录

2014-03-16 09:00:00
2014-03-08 09:00:00
2014-03-09 09:00:00
2014-03-15 10:00:00
2014-03-09 11:00:00
2014-03-08 11:00:00
2014-03-16 11:00:00
2014-03-10 12:00:00
2014-03-15 12:00:00

myDate log

 2014-03-16 16:00:00 +0000
 2014-03-08 16:00:00 +0000
 2014-03-09 16:00:00 +0000
 2014-03-15 17:00:00 +0000
 2014-03-09 18:00:00 +0000
 2014-03-08 18:00:00 +0000
 2014-03-16 18:00:00 +0000
 2014-03-10 07:00:00 +0000
 2014-03-15 07:00:00 +0000
 (null)

1 个答案:

答案 0 :(得分:2)

24小时制的日期格式为HH,而不是hh。并且,如所观察到的,在 What is the best way to deal with the NSDateFormatter locale "feechur"?,您应该添加

[df setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

独立于设备上的区域设置。