首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone NSDateComponent错误日期1.1.2011?

iPhone NSDateComponent错误日期1.1.2011?
EN

Stack Overflow用户
提问于 2010-07-10 17:31:37
回答 2查看 1.3K关注 0票数 3

我用“DateFromComponents”定了个日期,当我读出来的时候.这是错误的。我必须把日期定在第二天或以后的任何事情上,这样我才能得到正确的年份?!?我设定了2011年,当我读到它的时候,我得到了2010年!

代码语言:javascript
复制
 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克里斯

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-07-10 18:32:29

我运行了这个代码:

代码语言:javascript
复制
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);
}

..。得到了这个输出:

代码语言:javascript
复制
 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

显然,问题在于格式化程序,而不是日期。但是,我不认为格式化程序有什么问题。。

编辑:

移动:

代码语言:javascript
复制
[dateFormatter setDateFormat:@"DD MMMM YYYY"];

..。至:

代码语言:javascript
复制
[dateFormatter setDateFormat:@"DD MMMM yyyy"];

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

票数 6
EN

Stack Overflow用户

发布于 2010-07-11 09:06:39

正如http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns中所述,YYYY可能与日历年不同。

一月一日可以属于前一年的最后一周。当提到一年中的几周(即2009年的第52周)时,YYYY将显示所使用的年份。如果2010年1月1日属于2009年的第52周,那么YYYY将展示2009年。

所以你应该用yyyy。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3220176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档