开发者

iPhone NSDateComponent wrong Date 1.1.2011?

I set a Date with 'DateFromComponents" and when I read it out... its wrong. I have to set the DAY to the 2nd or anything later so I get the right Year?!? I set year 2011 and when I read it out i get Year 2010 !!


 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]);

------- Here I get 1 January 2010 /// 2010 no开发者_Python百科t 2011 !?!?

how to fix that.

thx chris


I ran this code:

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);
}

... and got this output:

 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

Clearly the problem is in the formatter and not the date. However, I don't see what could be wrong with the formatter. .

Edit:

Shifting:

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

... to:

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

... solves the problem although I don't know why.


As stated in http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns, YYYY can differ from the calendar year.

January 1st can belong to the last week of the year before. YYYY will display the year used when mentioning weeks of year (ie week 52 of 2009). If January 1st 2010 belong to the week 52 of 2009, then YYYY will display 2009.

That's why you should use yyyy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜