Date formatter giving wrong date when converting t

2019-08-28 06:10发布

问题:

I'm converting a date to text to display in a text box and it converts fine. The 29,30 and 31 December 2013 gets converted to 2014 and 29,30 and 31 December 2014 gets converted to 2015.

This is my code to convert the date to text

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM YYYY"];
self.pickupDateTextField.text = [dateFormatter stringFromDate:[self.datePicker date]];
NSLog(@"%@ %@",self.datePicker.date,self.pickupDateTextField.text);

This is what the NSLog is printing.

2013-12-29 06:58:14 +0000 29 Dec 2014

回答1:

The correct date formatter string is as below:

[dateFormatter setDateFormat:@"dd MMM yyyy"];

It should have small "yyyy" and not the capital "YYYY".