NSCalendar dateFromComponents:GMT 时区而不是 systemTimeZone

发布于 2024-12-03 09:28:54 字数 833 浏览 7 评论 0原文

此代码

NSCalendar *calendar =[NSCalendar currentCalendar];
[gregorian setTimeZone:tz];

NSLog(@"%@", [gregorian timeZone]);

NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:[NSDate date]];

[comp setDay:info.day];
[comp setMonth:info.month];
[comp setYear:info.year];
[comp setHour:info.hour];
[comp setMinute:info.minute];
[comp setSecond:info.second];
[comp setTimeZone:tz];  

NSLog(@"%@", [comp timeZone]);
NSLog(@"%@", [gregorian dateFromComponents:comp]);

在控制台中显示以下内容:

Europe/Moscow (MSKS) offset 14400 (Daylight)

Europe/Moscow (MSKS) offset 14400 (Daylight)

2011-08-31 20:00:00 +0000

因此,日历和组件的时区指定正确,但 [gregorian dateFromComponents:comp] 返回时区 (GMT) 错误的 NSDate 值。

我需要更正什么才能获得正确的时区?

This code

NSCalendar *calendar =[NSCalendar currentCalendar];
[gregorian setTimeZone:tz];

NSLog(@"%@", [gregorian timeZone]);

NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:[NSDate date]];

[comp setDay:info.day];
[comp setMonth:info.month];
[comp setYear:info.year];
[comp setHour:info.hour];
[comp setMinute:info.minute];
[comp setSecond:info.second];
[comp setTimeZone:tz];  

NSLog(@"%@", [comp timeZone]);
NSLog(@"%@", [gregorian dateFromComponents:comp]);

shows the following in console:

Europe/Moscow (MSKS) offset 14400 (Daylight)

Europe/Moscow (MSKS) offset 14400 (Daylight)

2011-08-31 20:00:00 +0000

So, the timezone for calendar and components is specified correctly, but [gregorian dateFromComponents:comp] returns NSDate value with wrong time zone (GMT).

What do I need to correct to get proper timezone?

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

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

发布评论

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

评论(2

安穩 2024-12-10 09:28:54

您看到的输出是完全正常的。如果直接 NSLog 一个 NSDate 对象,您将得到 description 方法返回的任何内容,这是日期的 GMT 表示(但这不是一成不变的)。

NSDate 对象不受时区的限制。 NSDate 表示从参考日期开始测量的绝对时刻。例如,在撰写本文时,当前日期类似于 337035053.199801(自参考日期以来的秒数),可以表示为 2011-09-06 20:50:53 GMT< /code> 或 2011-09-06 22:50:53 CEST。两者都是同一日期的不同人类可读表示。

总之,您需要什么才能获得正确的时区?您需要使用 NSDateFormatter 来获取您喜欢的任何时区的 NSDate 的字符串表示形式。

The output you are seeing is perfectly normal. If you NSLog a NSDate object directly, you will get whatever the description method returns, which is the GMT representation of the date (but that is not carved in stone).

NSDate objects are not subject to timezones. A NSDate represents an absolute instant in time measured from a reference date. For example, at the time of writing, the current date is something like 337035053.199801 (seconds since reference date), which can be represented as 2011-09-06 20:50:53 GMT or 2011-09-06 22:50:53 CEST. Both are different human readable representations of the same date.

In conclusion, what do you need to get the proper timezone? You need to use NSDateFormatter to get a string representation of your NSDate in any timezone you like.

晌融 2024-12-10 09:28:54

这个问题经常出现,答案是使用 NSDateFormatter 以获得日期格式所需的输出。目前,它始终打印 GMT 时区。以下是 NSDate

讨论
代表不保证保留
在不同版本的操作系统中保持不变。格式化
日期,您应该使用日期格式化程序对象来代替(请参阅
NSDateFormatter 和数据格式化指南)

This question comes up quite often and the answer is to use an NSDateFormatter to get the desired output for the date format. Currently it is always printed with the GMT timezone. Here is the discussion for the documentation for NSDate:

Discussion
The representation is not guaranteed to remain
constant across different releases of the operating system. To format
a date, you should use a date formatter object instead (see
NSDateFormatter and Data Formatting Guide)

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