NSDate、NSDateFormatter [duplicate]的怪异行为

时间:2022-01-21 17:26:14

This question already has an answer here:

这个问题已经有了答案:

I am formatting a NSDate in my application. Using below code:

我正在应用程序中格式化NSDate。使用下面的代码:

    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    NSString* currentDateTime = [dateFormatter stringFromDate:now];

    NSLog(@"CurrentDateTime:%@", currentDateTime);

    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    currentDateTime = [dateFormatter stringFromDate:now];
    NSLog(@"CurrentDateTime 1 :%@", currentDateTime);

    [dateFormatter release];

But this work fine for whole year but fails after 27-Dec. It adds a year after format the date of 27-Dec or above. Only when we use the "YYYY" instead of "yyyy". Its again works fine after the year change. So please can you suggest the reason behind this.

但这一招全年有效,12月27日以后就失效了。在格式为12月27日或以上后一年增加。只有当我们用“yyyyy”而不是“YYYY”的时候。经过一年的变化,它又能正常工作了。所以,请您提出这背后的原因。

1 个解决方案

#1


1  

My answer for your question is below

我对你的问题的回答如下

"YYYY" is week-based calendar year.

"yyyy" is ordinary calendar year.

For more details

为更多的细节

Difference between 'yyyy' and 'YYYY'

yyyy和yyyy的区别

Unicode Standard

Unicode标准

#1


1  

My answer for your question is below

我对你的问题的回答如下

"YYYY" is week-based calendar year.

"yyyy" is ordinary calendar year.

For more details

为更多的细节

Difference between 'yyyy' and 'YYYY'

yyyy和yyyy的区别

Unicode Standard

Unicode标准