服务器将返回正确的日期和时间。纽约时区。
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"发布于 2013-09-10 12:01:11
我现在的时间(那一刻)是:下午3点
纽约时间(那一刻)是上午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发布于 2013-09-10 10:51:48
你说“服务器回来了”。你能告诉我们你是如何得到你原来的约会的吗?您可能正在构建UTC日期,然后将其格式化为NY (东部时区)日期。复活节TZ是UTC-5.我猜你已经实行夏令了,因为5:37 - 5h的时间是0:37。
如果您发布了如何使用所示代码进行格式化的原始日期,我可能会得到更具体的信息。
https://stackoverflow.com/questions/18716536
复制相似问题