NSDateFormatter问题 [英] NSDateFormatter Issues

查看:85
本文介绍了NSDateFormatter问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于NSDateFormatter的问题. 也许我错过了一些东西,但是我的NSDateFormatter不能仅在时间上正确调整日期:

Hi I have a question regarding NSDateFormatter. Maybe Im missing something but my NSDateFormatter is not adjusting the date correctly only the time:

// NSDateFormatter (self.df) initialized in another method.
[self.df setDateFormat:@"MMM dd, YYYY hh:mm a"];
NSString *dpDate = [(UITextField*)[dateCell viewWithTag:101] text];
NSLog(@"tf date: %@",dpDate);
NSDate *date = [[NSDate alloc] init];
date = [self.df dateFromString:dpDate];
NSLog(@"Date: %@",[self.df stringFromDate:date]);

但是在打印后,日期似乎无法正确格式化以下是一些输出:

However after printing it seems the date doesn't format correctly Below are a few outputs:

咨询输出1:

xxx[5262:60b] tf date: Dec 05, 2013 09:35 AM

2013-12-05 09:35:17.718 xxx[5262:60b] Date: Dec 23, 2012 09:35 AM

咨询输出2:

xxx[5262:60b] tf date: Jan 16, 2014 09:45 AM

2013-12-05 09:36:21.151 xxx[5262:60b] Date: Dec 22, 2013 09:45 AM

此外,我只设置一次dateFormat.

In addition, I am only setting the dateFormat once.

推荐答案

您需要使用yyyy,而不是YYYY.第一个是常规年份(相对于配置的日历),第二个是基于基于周的日历年.

You need to use yyyy, not YYYY. The first is the regular year number (with respect to the configured calendar) and the second is for week-based calendar year.

有关如何使用NSDateFormatter的信息,请参见详细文章.两者之间的区别.

See this detailed article on how to use NSDateFormatter, and the distinction between the two.

这篇关于NSDateFormatter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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