本文介绍了从NSDate获取时区名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何从NSDate获取时区缩写(例如UTC,EET)的方法?我从字符串中获取NSDate,例如2012-08-26 02:54:50 +0200并且需要显示此日期的时区。

Any way to get timezone abbreviation(e.g UTC, EET) from NSDate? I'm getting NSDate from string, e.g 2012-08-26 02:54:50 +0200 and need to show timezone of this date.

目前这样做:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: @"yyyy-MM-dd HH:mm:ssZZZ"];
NSDate *isoDate = [formatter dateFromString: isoTime.value];
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMTForDate: isoDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT: offset];

但我得到GMT + 03:00的timeZone缩写,但它应该是EET。有什么办法吗?

But I'm getting GMT+03:00 for timeZone abbreviation, but it should be EET. Any way to get it?

推荐答案

NSDate 与时区无关;它本身并没有时区。因此,您无法从中获取时区。

An NSDate is time zone independent; it doesn't inherently have a time zone. You therefore can't get a time zone from it.

secondsFromGMTForDate:返回该时区的偏移量(默认情况下,在您的情况下,从指定日期的GMT。它返回有关 NSTimeZone 的信息,这可能取决于日期(例如,如果您的时区是为了纪念夏令时),而不是日期。

secondsFromGMTForDate: returns the offset of that time zone (the default one, in your case) from GMT at the specified date. It's returning information about the NSTimeZone which may depend on the date (eg, if your time zone honours daylight savings), not about the date.

这篇关于从NSDate获取时区名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 23:26