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

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

问题描述

我有一个 NSDate 对象.假设它代表1-10-2011"

NSDate *date = [df dateFromString:@"2011-10-01 00:00:00"];

由于我的时区,该日期转换为2011-09-30 22:00:00".

问题:如何在本地时区获取表示2011-10-01 00:00:00"的新日期对象?

解决方案

NSDate 仅代表一个绝对时间点.它没有时区或日历的概念.当您创建一个 NSDate 实例时,它距离格林威治标准时间 2001 年 1 月 1 日只有几秒​​钟的时间!无论您是在纽约、东京、巴塞罗那还是耶路撒冷,都没有关系.

在您的示例中,您根据 GMT 实例化 NSDate,但是 [date description](在 NSLog 中使用)将其转换为您的本地时间.这就是不匹配.

所以有两个部分需要考虑:

1.使用 NSCalendar 和 NSTimeZone 创建 NSDate

如果您要手动创建日期,则应指定日历(公历为 2012,希伯来语为 5772)和时区(伦敦时间下午 22 点,悉尼时间上午 7 点).

//使用用户当前的日历和时区NSCalendar *calendar = [NSCalendar currentCalendar];[日历 setTimeZone: [NSTimeZone systemTimeZone]];//手动指定日期组件(年、月、日、小时、分钟等)NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];[timeZoneComps setHour:22];[timeZoneComps setMinute:0];[timeZoneComps setSecond:0];//... 年,月,...//根据当前日历设置将日期组件转换为日期NSDate *date = [日历 dateFromComponents:timeZoneComps];

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

2.使用 NSDateFormatter 的 NSDate 输出

对于您的 NSDate 的受控输出,您需要 NSDateFormatter,它用于将日期转换为字符串.

基于苹果 NSDateFormatter 类参考 文档

<块引用>

您可以在 样式日期上获取和设置许多属性格式化程序, ...但是,我们鼓励您不要更改个别设置.相反,您应该接受初始化时建立的默认设置,并使用 setDateStyle:、setTimeStyle:

指定格式

这对输出来说特别重要,因为每个语言环境都不同.默认情况下 NSDateFormatter 观察当前用户的区域设置.因此,相同的 NSDate 可能是 22.11.2011 18:33:19,或 Nov 22, 2011 6:33:19 PM,或 2011-11-22下午6:33:19甚至२२-११-२०११६:३३:१९अपराह्,输入相同,代码相同.和代码:

//NSDate *date ->NSString *dateStringNSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateStyle:NSDateFormatterMediumStyle];[dateFormatter setTimeStyle:NSDateFormatterShortStyle];//中等样式日期,短样式时间 =>1937 年 11 月 23 日下午 3:30"NSString *dateString = [dateFormatter stringFromDate:date];

或者你可以使用类方法转换它localizedStringFromDate:dateStyle:timeStyle:

我希望这能澄清问题.

I have a NSDate object. Let's say it represents "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.

Question: How do I get a new Date object representing "2011-10-01 00:00:00" in my local timezone?

解决方案

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.

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:

1. NSDate creation using NSCalendar and NSTimeZone

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.

2. NSDate output using NSDateFormatter

For a controlled output of your NSDate you need NSDateFormatter, which is used to convert dates into strings.

Based on Apple NSDateFormatter Class Reference documentation

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:

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.

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:

I hope this clarifies the problem.

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

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