Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date conversion issue with objective C

Weird issue I'm having, if I do this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM d, yyyy EEEE 'at' h:mm a"];
NSString *originalDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"In Date: %@",originalDate);
NSDate *newDate = [dateFormatter dateFromString:originalDate];
NSLog(@"Out Date: %@",[dateFormatter stringFromDate:newDate]);

I get this output:

In Date: April 30, 2013 Tuesday at 2:42 PM
Out Date: December 28, 1999 Tuesday at 2:42 PM

Why am I getting different dates after converting it to string and back using the same formatter? Is there a way to get this conversion to work properly?

like image 285
Travis M. Avatar asked Jan 17 '26 05:01

Travis M.


1 Answers

This is a bug in iOS 6. NSDateFormatter can't properly parse date formats that have the weekday name anywhere besides the start of the string. I submitted a bug report for this a while ago. Still waiting for a fix.

The same format will work under iOS 5.

Update: Based on the test by vikingosegundo I update my own iOS tests. This appears to be the same issues as under OS X. It's not a simple matter of the weekday format being at the start, it is a bug if the weekday format appears after the year format.

like image 141
rmaddy Avatar answered Jan 19 '26 19:01

rmaddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!