iPhone NSDateComponent 日期错误 1.1.2011?

发布于 2024-09-09 02:34:09 字数 813 浏览 8 评论 0原文

我用“DateFromComponents”设置了一个日期,当我读出它时......它是错误的。 我必须将日期设置为第二天或以后的任何时间,这样我才能得到正确的年份?!? 我设置了 2011 年,当我读出它时,我得到了 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]);

------- 这里我得到1 January 2010 /// 2010 not 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 not 2011 !?!?

how to fix that.

thx chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2024-09-16 02:34:09

我运行了这段代码:

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

显然问题出在格式化程序而不是日期。但是,我不知道格式化程序有什么问题。 。

编辑:

Shifting:

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

... 到:

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

... 解决了问题,尽管我不知道为什么。

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.

木落 2024-09-16 02:34:09

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 的原因。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文