PDA

View Full Version : NSDate returning wrong date




stokedev
Dec 28, 2008, 02:26 PM
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.

http://www.tvstatus.com/datetester/Test1.jpg

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".


http://www.tvstatus.com/datetester/Test2.jpg

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!


http://www.tvstatus.com/datetester/Test3.jpg

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.


http://www.tvstatus.com/datetester/Test4.jpg

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.


http://www.tvstatus.com/datetester/Test5.jpg

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.


http://www.tvstatus.com/datetester/Test6.jpg

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. (http://www.tvstatus.com/datetester/DateTester.zip)

Let me know what you all are experiencing.

Thanks!



admanimal
Dec 28, 2008, 05:33 PM
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.

stokedev
Dec 28, 2008, 05:59 PM
Well it definitely looks like you found a bug, but the good (?) news is that it is in NSDateFormatter not NSDate. If you use on of the default formatter styles like NSDateFormatterFullStyle, it displays the correct date.

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!

gyokushou
Dec 29, 2008, 04:20 AM
Hi,
I got this problem too,
after a few tring I use yyyy instead of YYYY and it works!

rellik
Dec 29, 2008, 06:08 AM
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.

splashsoftware
Dec 29, 2008, 08:38 PM
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.

Pring
Dec 30, 2008, 06:06 PM
Is everyone who's getting the problem using 'YYYY'? As far as I know that's not valid at all.

You should use either 'yyyy' or 'Y'. Y refers to the 'ISO Week', see http://unicode.org/reports/tr35/#Date_Format_Patterns and http://en.wikipedia.org/wiki/ISO_week