ios - NSDateFormatter 问题

标签 ios objective-c nsdateformatter

您好,我有一个关于 NSDateFormatter 的问题。 也许我遗漏了一些东西,但我的 NSDateFormatter 没有正确调整日期,只有时间:

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

但是打印后日期格式似乎不正确下面是一些输出:

咨询输出 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。

最佳答案

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

查看此 detailed article关于如何使用 NSDateFormatter,以及两者的区别。

关于ios - NSDateFormatter 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20402935/

相关文章:

ios - 在静态iOS库中强制使用ARC

ios - 为什么我在 Swift playground 中进行简单的加法操作时会出错?

ios - Collection View 单元格边框颜色在 Swift 3 中取消选择

ios - Objective-C:后退按钮在从 AppsDelegate 呈现/推送 ViewController 后消失

ios - 在 XCode 中 react native 调试 iOS 组件

iOS 根据用户区域设置日期格式

ios - dat转换IOS报错

当前设备的启动图像的 IOS UIImage

ios - 用于可变日期类型字符串的 Swift dateFormat

ios - 将 View 放入 View Controller 后,该 View 的属性检查器完全空白。为什么会这样?