3

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

    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.

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
Mitesh Khatri
  • 3,935
  • 4
  • 44
  • 67

1 Answers1

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'

Unicode Standard

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39