Date formatter giving wrong date when converting to string Objective-C

时光总嘲笑我的痴心妄想 提交于 2020-01-06 14:04:43

问题


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



来源:https://stackoverflow.com/questions/20627993/date-formatter-giving-wrong-date-when-converting-to-string-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!