NSDateFormatter到NSString吗? [英] NSDateFormatter to NSString?

查看:70
本文介绍了NSDateFormatter到NSString吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSDate *dateNow = [NSDate date];
NSLog(@"NSDate                : %@", dateNow);

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];

NSString *dateString = [dateFormatter stringFromDate:dateNow];
NSLog(@"NSDateFormatter String: %@", dateString);

NSDate *dateObject = [dateFormatter dateFromString:dateString];
NSLog(@"NSDateFormatter Object: %@", dateObject);

首先,请忽略1970年1月1日,我现在只对这个时间感兴趣。

First off ignore the 1970-01-01 I am only interested in the time for now.

我的问题是,当我从NSDate获得时间时,它不包含任何时区信息。这就是我所期望的,所以从NSDate返回的时间是01:21:57 PM。若要更正此问题,我使用了NSDateFormatter并设置了其语言环境,它正确地解释了我在英国夏令时(GMT + 01)的伦敦。最后,我想将新的更正日期作为NSDate返回,所以我在先前正确的dateString上使用dateFromString。

My question is when I get the time from NSDate it does not include any time zone information. This is what I expected so the time returned from NSDate is 01:21:57 PM. To correct this I am using NSDateFormatter and setting its locale, it correctly interprets that I am in London, British Summer Time (GMT+01). Finally I want to get the new corrected date back as an NSDate so I use dateFromString on the previously correct dateString.

谁能告诉我为什么最终的日期恢复为未更正(即01:21:57 PM),我是否可以进行此转换并仍然保持指定的语言环境/ timeZone?

Can anyone tell me why the resultant date reverts back to being uncorrected (i.e. 01:21:57 PM) is there anyway that I can do this conversion and still maintain the specified locale / timeZone?

>> WALL_CLOCK: 02:21:57 PM
>> NSDate                : 2011-04-08 01:21:57 PM +0000
>> NSDateFormatter String: 02:21:57 PM GMT+01:00
>> NSDateFormatter Object: 1970-01-01 01:21:57 PM +0000


推荐答案

所有NSDate对象都存储为自引用日期(格林尼治标准时间2001年1月1日00:00)以来的秒数,因此始终是格林尼治标准时间+0。

All NSDate objects are stored as seconds since the reference date (Jan 1 2001 00:00 GMT) and so are always GMT+0.

这篇关于NSDateFormatter到NSString吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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