从 NSDate 获取 NSDate 调整时区 [英] Get NSDate from NSDate adjusted with timezone

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

问题描述

我有一个 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.

问题:如何在我的本地时区获取一个新的 Date 对象,该对象表示2011-10-01 00:00:00"?

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,但 [date description](在 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];

此时 date 存储代表当前日历的确切时间点(以秒为单位).

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 类参考 文档

Based on Apple NSDateFormatter Class Reference documentation

您可以在风格的日期上获取和设置许多属性格式化程序, ...但是,我们鼓励您不要更改个人设置.相反,您应该接受初始化时建立的默认设置并指定格式使用 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,或 Nov 22, 2011 6:33:19 PM,或 2011-11-22下午 6:33:19 甚至 २२-११-२०११ ६:३३:१९ अपराह्,所有这些都用于相同的输入和相同的代码.

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:

我希望这可以澄清问题.

I hope this clarifies the problem.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆