54

im using xcode 4.5(4G182) with iOS 6. NSDateFormatter show wrong year in iOS 6, how to solve?

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSString *str = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@ == %@",str,[[dateFormatter dateFromString:str] description]);

it print out "2012-09-14 == 2011-09-13 16:00:00 +0000"

Simon
  • 819
  • 1
  • 10
  • 18
  • 1
    I don't think this is an "iOS 6 issue". You should use lowercase `y`. See http://stackoverflow.com/questions/6388370/string-formatted-date-picker-date-is-off/6388430#6388430. –  Sep 14 '12 at 11:07

1 Answers1

163

YYYY is not the same as yyyy.

According to this page which the iOS Date format page references;

`y`: Year
`Y`: Year (in "Week of Year" based calendars). This year designation is used in 
     ISO year-week calendar as defined by ISO 8601, but can be used in 
     non-Gregorian based calendar systems where week date processing is desired. 
     May not always be the same value as calendar year.

The operative sentence being the last one. Use yyyyinstead.


Further details on how and why the year values may deviate when using YYYY:

The ISO week-numbering year starts at the first day (Monday) of week 01 and ends at the Sunday before the new ISO year (hence without overlap or gap). It consists of 52 or 53 full weeks. The ISO week-numbering year number deviates from the number of the traditional Gregorian calendar year on a Friday, Saturday, and Sunday, or a Saturday and Sunday, or just a Sunday, at the start of the traditional Gregorian calendar year (which are at the end of the previous ISO week-numbering year) and a Monday, Tuesday and Wednesday, or a Monday and Tuesday, or just a Monday, at the end of the traditional Gregorian calendar year (which are in week 01 of the next ISO week-numbering year). For Thursdays, the ISO week-numbering year number is always equal to the traditional Gregorian calendar year number.

Examples:

Monday 29 December 2008 is written "2009-W01-1"

Sunday 3 January 2010 is written "2009-W53-7"

From https://en.wikipedia.org/wiki/ISO_8601#Week_dates (bold styling added)

Community
  • 1
  • 1
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • 3
    Thanks, I've been crazy with this problem for a long time.. Thanks a lot – Vladimir Stazhilov Mar 27 '13 at 11:16
  • 5
    I think there's an element of thoughtlessness in designing a formatter that uses YYYY to format a year that "may not always be the same value as the calendar year." Wouldn't 'QQQQ' or 'XXXX' (for example) have been a less error-prone choice? "Mnemonic value" doesn't really seem like the thing to prioritize here. – Spike0xff Dec 29 '14 at 23:35
  • This was a fantastic solution. @Spike0xff has a great point about the frustration, and don't even know when/how I put YYYY in my code while having yyyy elsewhere. Could have been after PHP coding. Also I edited the answer with additional expository details. My code including YYYY always worked until yesterday when failing on epoch 1451401222, December 29, 2015 and it was coming up as December 2016. No other previous dates failed in my app using the same code. Thanks again, so much. – Tom Pace Jun 18 '16 at 13:54
  • 3
    I feel embarrassed, after 3+ years of experience in iOS and I just know it now!! – Hossam Ghareeb Jul 27 '16 at 10:38
  • Actually, you should use `y`, not `yyyy`, otherwise it will output `0476` instead of `476` for the fall of the Roman Empire. – Cœur May 19 '19 at 03:46
  • Thank you for the solution, I spent hours on this problem and figured out new years were starting on a monday and it was driving me crazy cause I didn't even know "Week of Year" based calendar were a thing. Thanks very much! – Alexis Coquard Dec 30 '19 at 16:22
  • 1
    Thanks this was driving me crazy since Dec 29, 30, and 31 of 2019 were showing up as 2020! – Locutus Jan 07 '20 at 02:32
  • 1
    I had this issue between Dec 2020 and 2021 ... thought I was taking crazy pills. – Tristian Jan 06 '21 at 10:28
  • Thank you my sir, i wasn't aware of this. – Jeremy Luisetti Jan 20 '21 at 17:46