iPhone NSDateComponent 日期错误 1.1.2011?
我用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我运行了这段代码:
...并得到了这个输出:
显然问题出在格式化程序而不是日期。但是,我不知道格式化程序有什么问题。 。
编辑:
Shifting:
... 到:
... 解决了问题,尽管我不知道为什么。
I ran this code:
... and got this output:
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:
... to:
... solves the problem although I don't know why.
如 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.