日期格式化程序在转换为字符串 Objective-C 时给出错误的日期 [英] Date formatter giving wrong date when converting to string Objective-C

查看:60
本文介绍了日期格式化程序在转换为字符串 Objective-C 时给出错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将日期转换为文本以显示在文本框中,并且转换得很好.2013 年 12 月 29、30 和 31 日转换为 2014 年,2014 年 12 月 29、30 和 31 日转换为 2015 年.

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);

这就是 NSLog 正在打印的内容.

This is what the NSLog is printing.

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

推荐答案

正确的日期格式化字符串如下:

The correct date formatter string is as below:

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

它应该有小写的yyyy"而不是大写的YYYY".

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

这篇关于日期格式化程序在转换为字符串 Objective-C 时给出错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆