Difference between #39;YYYY#39; and #39;yyyy#39; in NSDateFormatter(NSDateFormatter 中 YYYY 和 yyyy 的区别)
问题描述
What is exact difference between 'YYYY' and 'yyyy'. I read in this link, it states that
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.
But when I try to use
NSString *stringDate = @"Feb 28, 2013 05:30pm";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy hh:mma"];
NSDate *date=[dateFormatter dateFromString:stringDate];
NSLog(@"Date 1 : %@",date); //2013-02-28 12:00:00 +0000
NSString *stringDatee = @"Feb 28, 2013 05:30pm";
NSDateFormatter *dateFormatterr = [[NSDateFormatter alloc] init];
[dateFormatterr setDateFormat:@"MMM dd, YYYY hh:mma"];
NSDate *datee=[dateFormatterr dateFromString:stringDatee];
NSLog(@"Date 2 : %@",datee); //2013-01-05 12:00:00 +0000
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM dd, YYYY hh:mma"];
NSString *dateString = [dateFormat stringFromDate:datee];
NSLog(@"date 3 : %@", dateString); //Jan 05, 2013 05:30PM
As here, result to date
and datee
different, which I understood, but why result of date 2 and date 3 are different? As I am creating date from string and reversing same to string again, but output mismatches?
Has anybody knows reason about same?. Though it specifies week of year, still I should get result same.
Thanks..
EDIT :-
If I code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM dd, YYYY hh:mma"];
NSString *dateString = [dateFormatterr stringFromDate:[NSDate date]];
NSLog(@"date: %@", dateString); //Feb 28, 2013 04:37PM
If results me proper result, but same which I pass as string to date I get 2013-01-05 12:00:00 +0000
, check date 2 of NSLog, Strange result, why?
Also when using a date format string using the correct format is important.
@"YYYY" is week-based calendar year.
@"yyyy" is ordinary calendar year.
You can go through the whole blog, its a good to give it a look
https://web.archive.org/web/20150423093107/http://realmacsoftware.com/blog/working-with-date-and-time
http://realmacsoftware.com/blog/working-with-date-and-time (dead link)
这篇关于NSDateFormatter 中 'YYYY' 和 'yyyy' 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NSDateFormatter 中 'YYYY' 和 'yyyy' 的区别
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01