美文网首页iOS 开发
iOS 获取当前时间使用dateformatter出错?

iOS 获取当前时间使用dateformatter出错?

作者: 没有黑眼圈de熊猫 | 来源:发表于2015-12-29 17:03 被阅读699次

以前没问题 最近才出来

NSDate *now1 = [NSDate date];

NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];

[dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSString *locationString= [dateformatter stringFromDate:now1];

now1 得到时间是当前年份2015 loactionString 得到却是2016年

解决方法就是:把YYYY改成小写就好了

原因就是:DateFormatter 查了一下class reference,具体描述:

It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

就是说yyyy指定的是“日历年”,YYYY指定的是“星期年”,具体什么是日历年星期年的没去再找是什么,但打开日历似乎就明白了。所以日后注意DateFormatter里面的年份,你不会想用YYYY的~~~

相关文章

网友评论

    本文标题:iOS 获取当前时间使用dateformatter出错?

    本文链接:https://www.haomeiwen.com/subject/imkihttx.html