NSDate显示和时区的关系

在网上看到一篇介绍NSDate的博文。在它的“NSDate初始化“章节,说在使用  NSLog(@"当前时间 date = %@",[NSDate date]);时,显示出来的时间不是自己时区的时间,比我们小8个小时。因此判断该问题是时区问题。

好,我承认作者判断的很对,但是他给出的解决方法,却令人很意外。这个点对于理解[NSDate date]有很大的误导和迷惑性。

他的部分代码如下:

// 获取当前日期

NSDate *date = [NSDatedate];

// 打印结果: 当前时间date = 2013-08-16 09:00:04 +0000

NSLog(@"当前时间 date = %@",date);

// 获取从某个日期开始往前或者往后多久的日期,此处60代表60秒,如果需要获取之前的,将60改为-60即可

date = [[NSDate alloc]initWithTimeInterval:60 sinceDate:[NSDate date]];

//打印结果:当前时间 往后60s的时间date = 2013-08-16 09:01:04 +0000

NSLog(@"当前时间 往后60s的时间date = %@",date);

PS:测试时时间是下午5点,但是得到的当前时间却是上午9点,相差了8小时,是时区的问题

NSTimeZone *zone =[NSTimeZone systemTimeZone];

NSInteger interval = [zonesecondsFromGMTForDate: date];

NSDate *localDate =[date  dateByAddingTimeInterval: interval];

// 打印结果 正确当前时间 localDate = 2013-08-16 17:01:04 +0000

NSLog(@"正确当前时间 localDate = %@",localDate);

因为我们在正8区,而使用NSLog显示[NSDatedate]时,显示的时间是GTM时区的时间。

作者将[NSDate date]的结果加上8×60×60,得到的时间就是我们正8区看到的时间。

好吧,不得不说这个方法得到的结果是符合要求的,显示时间数字是一致的了。

下面说这个方法的不当之处。它给人一种误导,[NSDate date]得到的时间是应该加8小时,才能正确显示符合我们时区的时间。

何必呢?不就是要[NSDate date]符合本时区的时间数字显示出来吗?何必认为修改时间数值呢?

NSDate对象的显示结果不符合,却去修改对象本身,真真是舍本逐末。

刚好在*.com上找到一帖,可以用来解释这个问题。呵呵。

http://*.com/questions/4547379/nsdate-is-not-returning-my-local-time-zone-default-time-zone-of-device

My local and default time zone is GMT +5 but when I get date and time byNSDate it return me GMT date and time.

For example the code and output from my code while testing on device isas, [device time zone Islamabad GMT +5]

NSTimeZone *lo = [NSTimeZonelocalTimeZone];

NSLog(@" - current  local timezone  is %@",lo); // GMT +5

2010-12-28 20:56:11.785 Done[484:307]  - current local timezone  is  Local Time Zone (Asia/Karachi (GMT+05:00)offset 18000)

NSTimeZone *df = [NSTimeZonedefaultTimeZone];

NSLog(@" - current  default timezone  is %@",df); // GMT +5

2010-12-28 20:56:11.790 Done[484:307]  - current default timezone  is  Asia/Karachi (GMT+05:00) offset 18000

NSDate *cDate = [NSDatedate];

NSLog(@"current date byNSDate %@",cDate); //but NSDate show GMT

2010-12-28 20:56:11.794 Done[484:307] current date by NSDate2010-12-28 15:56:11 GMT

NSDateFormatter*dateFormatter = [[NSDateFormatter alloc] init];

dateFormatter.dateFormat =@"yyyy-MM-dd'T'HH:mm";

//// NSTimeZone *gmt =[NSTimeZone ]

NSTimeZone *gmt = [NSTimeZonetimeZoneWithAbbreviation:@"GMT+05:00"];

[dateFormattersetTimeZone:gmt];

NSString *timeStamp =[dateFormatter stringFromDate:[NSDate date]];

NSLog(@" date stringobject  %@" ,timeStamp);   // string From Date is GMT +5

2010-12-28 20:56:11.802 Done[484:307]  date string object  2010-12-28T20:56

NSDate *datef =[dateFormatter dateFromString:@"2010-12-28T20:56"];

NSLog(@" date object%@" ,datef);  // the date form abovestring gives again GMT

2010-12-28 20:56:11.809 Done[484:307]  **date object 2010-12-28 15:56:00 GMT**

Why is NSDate not giving local current time? Please help...

这是一个巴基斯坦人的提问,他不明白为什么会这样?您明白了吗?你知道了怎么来正确地显示时间数字了吗?

上一篇:不完善的css怦然心动,有待改进...


下一篇:如何高效地写CSS--等以后有空多加总结一下