IOS:NSDate 的问题 [英] IOS: problem with NSDate
本文介绍了IOS:NSDate 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码:
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:4];
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDate = [gregorianCalendar dateFromComponents:components];
NSLog(@"date:%@", firstDate);
控制台的结果是:
date:2011-03-31
为什么????如果我设置setDay:1",它会是date:2011-04-01",不是吗?
why???? if I set "setDay:1" it would be "date:2011-04-01", no?
推荐答案
我认为这可能是与时区相关的问题.请务必在 NSDateComponents 上设置您的时区:
I think this could be a time zone related problem. Be sure to set your time zone on NSDateComponents:
[ components setTimeZone:[ NSTimeZone systemTimeZone ] ] ;
这篇关于IOS:NSDate 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文