ios 从 NSDate 获取 NSDate 调整时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7485493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get NSDate from NSDate adjusted with timezone
提问by Tycho Pandelaar
I have a NSDate object. Let's say it represents "1-10-2011"
我有一个 NSDate 对象。假设它代表“1-10-2011”
NSDate *date = [df dateFromString:@"2011-10-01 00:00:00"];
That date translates into "2011-09-30 22:00:00" because of my timezone.
由于我的时区,该日期转换为“2011-09-30 22:00:00”。
Question: How do I get a new Date object representing "2011-10-01 00:00:00" in my local timezone?
问题:如何在我的本地时区获取一个新的 Date 对象,表示“2011-10-01 00:00:00”?
回答by Carlos Barcelona
NSDateonly represents an absolute point in time. It has no concept of timezone or calendar. When you create a NSDate instance it is just a number of seconds since January 1st 2001 GMT! It does not matter if you are in New York, Tokyo, Barcelona or Jerusalem.
NSDate只代表一个绝对的时间点。它没有时区或日历的概念。当您创建一个 NSDate 实例时,它距离格林威治标准时间 2001 年 1 月 1 日只有几秒钟的时间!无论您是在纽约、东京、巴塞罗那还是耶路撒冷,都没有关系。
At your example, you instance the NSDate based on GMT, but [date description]
(used in NSLog
) translates it into your local time. There you have the mismatch.
在您的示例中,您根据 GMT 实例化 NSDate,但[date description]
(在 中使用NSLog
)将其转换为您的本地时间。你有不匹配。
So there are two parts to consider:
所以有两个部分需要考虑:
1. NSDate creation using NSCalendar and NSTimeZone
1. 使用 NSCalendar 和 NSTimeZone 创建 NSDate
If you are creating a date manually you should specify the calendar (2012 in Gregorian, but 5772 in Hebrew) and time zone (22PM London time, but 7AM Sydney time).
如果您要手动创建日期,则应指定日历(公历 2012 年,希伯来语 5772)和时区(伦敦时间下午 22 点,悉尼时间上午 7 点)。
// Use the user's current calendar and time zone
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
// Specify the date components manually (year, month, day, hour, minutes, etc.)
NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];
[timeZoneComps setHour:22];
[timeZoneComps setMinute:0];
[timeZoneComps setSecond:0];
// ... year, month, ...
// transform the date compoments into a date, based on current calendar settings
NSDate *date = [calendar dateFromComponents:timeZoneComps];
At this point date stores the exact point in time (in seconds) representing the current calendar.
此时 date 存储表示当前日历的确切时间点(以秒为单位)。
2. NSDate output using NSDateFormatter
2. NSDate 输出使用 NSDateFormatter
For a controlled outputof your NSDate you need NSDateFormatter, which is used to convert dates into strings.
对于NSDate的受控输出,您需要NSDateFormatter,它用于将日期转换为字符串。
Based on Apple NSDateFormatter Class Referencedocumentation
基于 Apple NSDateFormatter Class Reference文档
There are many attributes you can get and set on a style date formatter, ... You are encouraged, however, not to change individual settings. Instead you should accept the default settings established on initialization and specify the formatusing setDateStyle:, setTimeStyle:
您可以在样式日期格式化程序上获取和设置许多属性,......但是,我们鼓励您不要更改单个设置。相反,您应该接受初始化时建立的默认设置,并使用 setDateStyle:、setTimeStyle指定格式:
This is specially important for the output, which is different for every locale. By default NSDateFormatter observes the current user's locale settings. So the same NSDate could be 22.11.2011 18:33:19
, or Nov 22, 2011 6:33:19 PM
, or 2011-11-22 下午6:33:19
or even ??-??-???? ?:??:?? ??????
, all for the same input and with the same code.
这对于输出特别重要,因为每个语言环境都不同。默认情况下 NSDateFormatter 观察当前用户的区域设置。因此,相同的 NSDate 可以是22.11.2011 18:33:19
、或Nov 22, 2011 6:33:19 PM
、或2011-11-22 下午6:33:19
什至??-??-???? ?:??:?? ??????
,都用于相同的输入和相同的代码。
And the code:
和代码:
// NSDate *date -> NSString *dateString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
// Medium style date, short style time => "Nov 23, 1937 3:30pm"
NSString *dateString = [dateFormatter stringFromDate:date];
Or you could transform it using the class method localizedStringFromDate:dateStyle:timeStyle:
或者你可以使用类方法localizedStringFromDate:dateStyle:timeStyle转换它:
I hope this clarifies the problem.
我希望这能澄清问题。
回答by zaph
[NSDate date]
returns the date in GMT.
[NSDate date]
以格林威治标准时间返回日期。
When you state:
当您声明:
That date translates into "2011-09-30 22:00:00" because of my timezone.
由于我的时区,该日期转换为“2011-09-30 22:00:00”。
Is that from NSLog
or NSDateFormatter
? Don't rely in [date description]
which NSLog uses, it takes into account your local timezone, use NSDateFormatter
. NSDateFormatter
has a setTimeZone
method.
是从NSLog
还是NSDateFormatter
?不要依赖[date description]
NSLog 使用哪个,它会考虑您的本地时区,使用NSDateFormatter
. NSDateFormatter
有setTimeZone
方法。
From Apple docs on [date description]
:
来自 Apple 文档[date description]
:
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
不能保证该表示在操作系统的不同版本中保持不变。要格式化日期,您应该改用日期格式化程序对象