NSDate returning wrong date

Discussion in 'iOS Programming' started by stokedev, Dec 28, 2008.

  1. stokedev macrumors newbie

    Dec 28, 2008
    It has come to my attention that NSDate returns the wrong date for days near the end of the year. This is a major issue since my application depends on NSDate values in order to operate properly meaning that my application is have problems that I cannot correct.

    Let me explain with the use of these examples. I created a simple example using the following code to display today's date:

    // Create the date formatter
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE, MMMM d, YYYY"];
    // Set the dateLabel on the view to today's date using the date formatter
    NSDate *today = [NSDate date];
    dateLabel.text = [dateFormatter stringFromDate:today];
    This is a rather simple segment of code which uses [NSDate date] to get the date value for today. dateLabel is the UILabel that appears on the view in the following series of pictures.


    Notice in this first image that the date is manually set to 12/25/2008 on the Mac's "Date & Time" option in the control panel. Whatever is set here is what the iPhone simulator uses as its time and date. Notice also that the date is printed properly by the date formatter: "Thursday, December 25, 2008".


    This image shows the problem that is occurring. The date is manually set to 12/29/2008 and the application is restarted in the simulator. This time, the date is printed wrong! The date shows up as "Monday, December 29, 2009" which is just wrong! That date does not even exist!


    This image shows the date functioning properly again. If the date is manually set to 1/1/2009 then it displays "Thursday, January 1, 2009" which is correct.


    This image is yet another test but for next year. The date is manually set to 12/30/2009 and the application is relaunched. This time the date shows up as "Wednesday, December 30, 2010" which is once again a none existent date and incorrect.


    Yet another test to prove my point. The date is manually set to 12/31/2011 and the date is showing up correctly. It is important to note that the problem only seems to occur during the week of New Year's meaning that the date problem just started occurring today, 12/28/2008, since the New Year begins on Thursday.


    Final picture to prove the bug. The date is set automatically by the Mac so the date is set to 12/28/2008. Notice though that the date shows up as "Sunday, December 28, 2009" instead of 2008.

    All in all, this is a huge flaw in the NSDate functionally and I'm rather confused as to how something like this could be happening. I just wanted to point out this problem to other Developers to see if others have noticed this issue.

    You can download the XCode project I used to create these screen shots to test the problem yourself. I have not actually run the code on the phone but know that the same problem is occurring since my application has been producing weird results. If I manually set the date forward to January 1st, 2009 though the problems go away.

    Here is the link for the code.

    Let me know what you all are experiencing.

  2. admanimal macrumors 68040

    Apr 22, 2005
    Well it definitely looks like you found a bug, but the good (?) news is that it is in NSDateFormatter not NSDate. If you use one of the default formatter styles like NSDateFormatterFullStyle, it displays the correct date.
  3. stokedev thread starter macrumors newbie

    Dec 28, 2008
    Great find! I did not even think about the fact that the NSDateFormatter could be causing the problem and not NSDate. Its still a strange little bug though. I guess I'll rework some of my code to avoid custom NSDateFormatter styles when possible. Thanks!
  4. gyokushou macrumors newbie

    Aug 25, 2008
    I got this problem too,
    after a few tring I use yyyy instead of YYYY and it works!
  5. rellik macrumors newbie

    Jun 23, 2008
    I too have found this problem. It has caused no end of problems, and some bad reviews with my app.

    To make matters worse it only seems to manifest itself on some devices - 8Gb devices seem to be most affected. To fix the issue I wrote my own datetime parser/formater and uploaded the fix to Apple just before Christmas.
  6. splashsoftware macrumors newbie

    Dec 29, 2008
    Hard to believe

    I too have experienced this problem. It seems to be only for the 29th, 30th and 31st.

    This has caused me a great deal of stress and I am very surprised that a problem like this can exist.
  7. Pring macrumors 6502

    Sep 17, 2003

Share This Page