2010-07-10 107 views
3

我用DateFromComponents设置了一个日期,当我读出它的错误时 我必须将DAY设置为第二或者以后的任何东西,这样我才能得到正确的一年?!?? I set year 2011 and当我读出来,我得到2010年!iPhone NSDateComponent错日期2011年1月1日?


day = 1; 
month = 1; 
year = 2011; 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *components = [[NSDateComponents alloc] init]; 
[components setDay:day]; 
[components setMonth:month]; 
[components setYear:year]; 
NSDate *date = [gregorian dateFromComponents:components]; 
[gregorian release]; 


NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter setDateFormat:@"DD MMMM YYYY"]; 
NSLog (@"hmm: %@",[dateFormatter stringFromDate:actDate]); 

-------这里我得到1 January 2010 /// 2010 not 2011!?!?

如何解决。

THX chris

+0

我有完全相同的问题,同一天甚至! YYYY - > yyyy以日期格式固定它。我希望我的生命中的那些小时回来。 – Bogatyr 2011-03-01 13:34:58

回答

6

我跑这个代码:

NSInteger day = 1; 
NSInteger month = 1; 
NSInteger year =0; 
NSCalendar *gregorian; 
NSDateComponents *components; 
NSDate *theDate; 
NSDateFormatter *dateFormatter; 
for (int i=2005; i<2015; i++) { 
    year= i; 
    gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    components = [[NSDateComponents alloc] init]; 
    [components setDay:day]; 
    [components setMonth:month]; 
    [components setYear:year]; 
    theDate = [gregorian dateFromComponents:components]; 
    [gregorian release]; 


    dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"DD MMMM YYYY"]; 
    NSLog(@"year=%d",year); 
    NSLog (@"hmm: %@",[dateFormatter stringFromDate:theDate]); 
    NSLog(@"theDate=%@\n\n",theDate); 
} 

...并得到了这个输出:

year=2005 
hmm: 01 January 2004 
theDate=2005-01-01 00:00:00 -0600 

year=2006 
hmm: 01 January 2006 
theDate=2006-01-01 00:00:00 -0600 

year=2007 
hmm: 01 January 2007 
theDate=2007-01-01 00:00:00 -0600 

year=2008 
hmm: 01 January 2008 
theDate=2008-01-01 00:00:00 -0600 

year=2009 
hmm: 01 January 2008 
theDate=2009-01-01 00:00:00 -0600 

year=2010 
hmm: 01 January 2009 
theDate=2010-01-01 00:00:00 -0600 

year=2011 
hmm: 01 January 2010 
theDate=2011-01-01 00:00:00 -0600 

year=2012 
hmm: 01 January 2012 
theDate=2012-01-01 00:00:00 -0600 

year=2013 
hmm: 01 January 2013 
theDate=2013-01-01 00:00:00 -0600 

year=2014 
hmm: 01 January 2014 
theDate=2014-01-01 00:00:00 -0600 

问题显然是在格式化,而不是日期。但是,我没有看到格式化程序可能会出现什么问题。 。

编辑:

转向:

[dateFormatter setDateFormat:@"DD MMMM YYYY"]; 

...到:

[dateFormatter setDateFormat:@"DD MMMM yyyy"]; 

...解决了这个问题,虽然我不知道为什么。

+1

:)完整答案在这里,也不知道: http://stackoverflow.com/questions/3220176/iphone-nsdatecomponent-wrong-date-1-1-2011/3222483#3222483 thx求助 – 2010-07-11 14:26:22

4

正如http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns指出,YYYY可以从历年不同。

1月1日可属于前最后一周的一年。 YYYY将显示提及几周(即2009年第52周)时使用的年份。如果2010年1月1属于周2009年的52,然后YYYY将显示2009年

这就是为什么你应该使用YYYY。

+0

非常感谢。 ..工作正常:) – 2010-07-11 14:25:34

+0

这是巨大的 - 感谢这一点,这是快把我逼疯了! – Bogatyr 2011-03-01 13:34:10

相关问题