为什么我的时区没有保存到我的 NSDate 中?

发布于 2024-12-27 02:20:46 字数 721 浏览 3 评论 0原文

我必须从 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 的日期,即使我将其时区设置为“欧洲/布达佩斯”。我该如何解决这个问题?

谢谢!

I must initialize an NSDate object from NSString in objective-c. I do it like this:

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];

E.g: when I try it with string value @"2011-01-02 17:49:54" I get an NSDate 2011-01-02 16:49:54 +0000. As you can see there is a one hour difference between the two values. NSDate has a wrong value, it should be exactly the same I defined in my string in the timezone I set in dateFormatter. It seems it uses my date defined it string as UTC, even if I set its timezone to "Europe/Budapest". How can I fix this problem?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2025-01-03 02:20:46

NSDate 存储相对于标准参考日期的日期。来自类文档:

“NSDate 的唯一原始方法 timeIntervalSinceReferenceDate 为 NSDate 接口中的所有其他方法提供了基础。该方法返回相对于绝对参考日期(格林威治标准时间 2001 年 1 月 1 日的第一个时刻)的时间值”。

NSDate 本身没有任何时区的概念。所以 NSDateFormatter 做了正确的事情:它转换了您告诉它具有 GMT 偏移量的日期(通过指定时区),并为您提供了该日期的“标准化”NSDate。

如果您想查看以欧洲/布达佩斯时区表示的日期,请使用现有的日期格式化程序 (-stringFromDate:) 或适当的 NSDate 描述方法(例如 -descriptionWithCalendarFormat:timeZone:locale:)。

PS-您在编写的代码中根本不需要分配/初始化。在非 ARC 中这将是泄漏。

PPS-您的日期格式不正确并且给出了无意义的结果。我已经按照如下方式清理了您的代码(在 ARC 下测试):

NSString *dateString = @"2011-09-02 17:49:54";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"Europe/Budapest"];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:tz];

NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"%@", [dateFormatter stringFromDate:date]);
NSLog(@"%@", [date descriptionWithCalendarFormat:nil timeZone:tz locale:nil]);

NSDate stores dates relative to a standard reference date. From the class docs:

"The sole primitive method of NSDate, timeIntervalSinceReferenceDate, provides the basis for all the other methods in the NSDate interface. This method returns a time value relative to an absolute reference date—the first instant of 1 January 2001, GMT."

NSDate does not itself have any concept of time zones. So the NSDateFormatter did the right thing: it converted a date which you told it had a GMT offset (by specifying a time zone), and gave you a "normalized" NSDate for that date.

If you want to see the date represented in the Europe/Budapest time zone, either use your existing date formatter (-stringFromDate:) or the appropriate NSDate description method (e.g. -descriptionWithCalendarFormat:timeZone:locale:).

P.S.- You don't need an alloc/init at all in your code as written. In non-ARC that would be a leak.

P.P.S.- Your date format is incorrect and giving nonsensical results. I've gone ahead and cleaned up your code as follows (tested under ARC):

NSString *dateString = @"2011-09-02 17:49:54";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"Europe/Budapest"];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:tz];

NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"%@", [dateFormatter stringFromDate:date]);
NSLog(@"%@", [date descriptionWithCalendarFormat:nil timeZone:tz locale:nil]);
找个人就嫁了吧 2025-01-03 02:20:46

有两件事:

1)日期格式字符串有错误。您应该使用 MM 表示月份,而不是 mm (小写 mm 表示分钟)

2) 创建 NSDate 对象后,您需要使用 NSDateFormatter 方法 stringFromDate: 生成本地化到特定时区的日期字符串。如果您只是在 NSDate 对象上直接执行 NSLog() ,默认情况下它将显示 GMT 日期(GMT 比布达佩斯时间晚一小时)

Two things:

1) you have an error in your date format string. You should use MM for month, not mm (lowercase mm is for minutes)

2) after you create you NSDate object, you'll need to use the NSDateFormatter method stringFromDate: to generate a date string localized to a particular timezone. If you just do a straight NSLog() on the NSDate object it will show the date as GMT by default (GMT is one hour behind Budapest time)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文