gpt4 book ai didi

iphone - NSTimeZone timeZoneWithName : works not properly

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:08:54 25 4
gpt4 key购买 nike

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

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"

最佳答案

我现在的时间(那一刻)是:下午 3:00

纽约时间(此时)是:8:00 AM

解决方案:

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

关于iphone - NSTimeZone timeZoneWithName : works not properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18716536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com