NSTimeZone timeZoneWithName:工作不正常

时间:2013-09-10 10:30:23

标签: iphone ios timezone

服务器返回正确的日期&时间。纽约时区。

2013-09-10 05:37:07 +0000(正确的时间,发布评论时)

以下是我在应用中对其进行格式化的方法:

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

[format setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];

结果我有

10 sept, 2013 01:37

怎么了?

UPD

代码

    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
    [format setDateFormat:@"dd MMM, yyyy HH:mm"];

    NSString *stringDate = [format stringFromDate:Date]; //Date is "2013-09-10 05:37:07 +0000"
    //stringDate is "10 sept, 2013 01:37"

2 个答案:

答案 0 :(得分:3)

我当前的时间(当时)是:下午3:00

纽约时间(当时)是:上午8点

解决方案:

NSDate *date = activityDate; // Server sends me the date: 2013-09-10 09:00:00 +0000
NSString *dateFormat = @"dd MMM, yyyy HH:mm";
//Then the transformation comes here
NSDateFormatter* newYorkDf = [[NSDateFormatter alloc] init];
[newYorkDf setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[newYorkDf setDateFormat:dateFormat];

NSDateFormatter* gmtDf = [[NSDateFormatter alloc] init];
[gmtDf setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[gmtDf setDateFormat:dateFormat];
NSDate *gmtDate = [gmtDf dateFromString:[newYorkDf stringFromDate:date]];

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:dateFormat];
NSString *stringDate1 = [format stringFromDate:gmtDate];
self.labelDate.text = stringDate1;   // Returns me: 10 Sept, 2013 8:00

答案 1 :(得分:0)

你说“服务器返回”。您能告诉我们更多关于您如何获得原始日期的信息吗? 您可能正在构建UTC日期,然后将其格式化为好像是纽约(东部时区)。复活节TZ是UTC-5。我猜你从5:37开始实行夏令时 - 5h会给0:37。

如果您发布了如何使用显示的代码构建原始日期,我可能会更具体。