【发布时间】:2023-03-16 07:04:01
【问题描述】:
我必须在objective-c 中从NSString 初始化一个NSDate 对象。我是这样做的:
NSString *dateString = [[webSentence child:@"DateTime"].text stringByReplacingOccurrencesOfString:@"T" withString:@" "];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-mm-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Budapest"]];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
例如:当我尝试使用字符串值 @"2011-01-02 17:49:54" 时,我得到一个 NSDate 2011-01-02 16:49:54 +0000。如您所见,这两个值之间存在一小时的差异。 NSDate 的值错误,它应该与我在 dateFormatter 中设置的时区中的字符串中定义的完全相同。即使我将其时区设置为“欧洲/布达佩斯”,它似乎也使用我将其定义为 UTC 的日期字符串。我该如何解决这个问题?
谢谢!
【问题讨论】:
标签: objective-c timezone nsdate nsdateformatter