从NSDate获取NSDate随时区调整 [英] Get NSDate from NSDate adjusted with timezone
问题描述
我有一个NSDate对象。假设它代表1-10-2011
I have a NSDate object. Let's say it represents "1-10-2011"
NSDate *date = [df dateFromString:@"2011-10-01 00:00:00"];
由于我的时区,该日期转换为2011-09-30 22:00:00。
That date translates into "2011-09-30 22:00:00" because of my timezone.
问题:如何在当地时区获得代表2011-10-01 00:00:00的新Date对象?
Question: How do I get a new Date object representing "2011-10-01 00:00:00" in my local timezone?
推荐答案
NSDate 仅代表绝对时间点。它没有时区或日历的概念。创建NSDate实例时,自2001年1月1日格林威治标准时间起,它只是几秒钟!如果你在纽约,东京,巴塞罗那或耶路撒冷都没关系。
NSDate only 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.
在你的例子中,你根据GMT实例化了NSDate,但 [日期描述]
(用于 NSLog
)将其转换为您当地的时间。那里有你的不匹配。
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.
所以有两个部分需要考虑:
So there are two parts to consider:
如果您手动创建日期,则应指定日历(2012年格里高利时,但5772希伯来语)和时区(伦敦时间晚上22点) ,但悉尼时间早上7点。。
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).
// 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.
对于NSDate的受控输出,您需要 NSDateFormatter ,用于转换日期字符串。
For a controlled output of your NSDate you need NSDateFormatter, which is used to convert dates into strings.
基于Apple NSDateFormatter类参考文档
你有很多属性可以获取并设置样式日期
格式化程序,...
但是,我们鼓励您不要更改个别设置。相反,你应该接受在初始化时建立的默认设置和使用setDateStyle指定格式:setTimeStyle:
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 format using setDateStyle:, setTimeStyle:
这对于输出特别重要,对于每个区域设置都是不同的。默认情况下,NSDateFormatter会观察当前用户的区域设置。因此相同的NSDate可能 22.11.2011 18:33:19
,或 2011年11月22日下午6:33:19
,或 2011-11-22下午6:33:19
甚至 22-11-2011 6:33:1 9अपराह्
,全部用于相同的输入和相同的代码。
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.
代码:
// 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];
或者您可以使用类方法对其进行转换 localizedStringFromDate:dateStyle:timeStyle:
Or you could transform it using the class method localizedStringFromDate:dateStyle:timeStyle:
我希望这可以澄清问题。
I hope this clarifies the problem.
这篇关于从NSDate获取NSDate随时区调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!