Objective-c : 如何将 PST 转换为 UTC/local 时区
Objective-c : How to convert PST to UTC/local time zone
我从服务器获取的时间戳为“2015-11-12T03:54:52Z”,采用太平洋标准时间。我想将此字符串转换为 UTC/Local 时区格式。我尝试了以下方式
NSDateFormatter *pstFormatter = [[NSDateFormatter alloc] init];
[pstFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]];
[pstFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *pstQUU = [pstFormatter dateFromString:@"2015-11-12T04:03:03Z"];
Original output : 2015-11-12 04:03:03 +0000
Expected output : 2015-11-12 12:03:03 +0000 (UTC)
尝试像这样添加 UTC 偏移值
NSDateFormatter *pstFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *pstTimezone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
[pstFormatter setTimeZone:pstTimezone];
[pstFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *pstDate = [pstFormatter dateFromString:@"2015-11-12T04:03:03Z"];
NSTimeInterval utcSeconds = [pstDate timeIntervalSince1970] - [pstTimezone secondsFromGMT];
NSDate *utcDate = [NSDate dateWithTimeIntervalSince1970:utcSeconds];
我从服务器获取的时间戳为“2015-11-12T03:54:52Z”,采用太平洋标准时间。我想将此字符串转换为 UTC/Local 时区格式。我尝试了以下方式
NSDateFormatter *pstFormatter = [[NSDateFormatter alloc] init];
[pstFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]];
[pstFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *pstQUU = [pstFormatter dateFromString:@"2015-11-12T04:03:03Z"];
Original output : 2015-11-12 04:03:03 +0000
Expected output : 2015-11-12 12:03:03 +0000 (UTC)
尝试像这样添加 UTC 偏移值
NSDateFormatter *pstFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *pstTimezone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
[pstFormatter setTimeZone:pstTimezone];
[pstFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *pstDate = [pstFormatter dateFromString:@"2015-11-12T04:03:03Z"];
NSTimeInterval utcSeconds = [pstDate timeIntervalSince1970] - [pstTimezone secondsFromGMT];
NSDate *utcDate = [NSDate dateWithTimeIntervalSince1970:utcSeconds];