NSCalendar dateFromComponents:GMT 时区而不是 systemTimeZone
此代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看到的输出是完全正常的。如果直接 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 thedescription
method returns, which is the GMT representation of the date (but that is not carved in stone).NSDate
objects are not subject to timezones. ANSDate
represents an absolute instant in time measured from a reference date. For example, at the time of writing, the current date is something like337035053.199801
(seconds since reference date), which can be represented as2011-09-06 20:50:53 GMT
or2011-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 yourNSDate
in any timezone you like.这个问题经常出现,答案是使用
NSDateFormatter
以获得日期格式所需的输出。目前,它始终打印 GMT 时区。以下是NSDate
: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 forNSDate
: