NSDateFormatter 中“ YYYY”和“ YYYY”的区别

“ YYYY”和“ YYYY”的确切区别是什么

一个常见的错误是使用 YYYY.YYYY 指定日历年 而 YYYY 指定的年份(“年度周”) ,在国际标准化组织使用 在大多数情况下,YYYY 和 YYYY 产生相同的 数字,但它们可能有所不同。通常,您应该使用 日历年。

但是当我尝试使用

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

至于这里,结果到 datedatee不同,这我明白,但为什么结果日期2和日期3不同?当我创建日期从字符串和反转相同的字符串再次,但输出不匹配?

有谁知道原因一样吗? 虽然它规定了一年的周,但我还是应该得到相同的结果。

谢谢。

编辑:-

如果我编码

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

如果结果我正确的结果,但同样的我传递字符串到日期我得到 2013-01-05 12:00:00 +0000,检查日期2的 NSLog,奇怪的结果,为什么?

67247 次浏览

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

enter image description here

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)

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.

from Apple Docs

dd/MMM/YYYY - e.g.:1 01/Jan/2000; answer : 19/dec/1999
(see weekly calendar December month last Monday
suppose leaf year + 1 day)
dd/MMM/yyyy - eg: ordinary - no problem.

All answers differentiating yyyy and YYYY are right answers for another question. The question itself refers to another thing.

Why does these two values are different? (extracted from question)

NSLog(@"Date 2 : %@",datee); //2013-01-05 12:00:00 +0000
NSLog(@"Date 3 : %@", dateString); //Jan 05, 2013 05:30PM

The answer here @P.J is that they are not really different in value. When you log an NSDate (which is Date 2) you are getting the full description of your object which happens to be on UTC Timezone. This logic does not happen when logging Date 3 because it was already converted to a String and applied your Timezone.

For printing Date 3 the 'same way' as you are getting Date 2. You should specify UTC TimeZone for Date 3. Something like this :

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM dd, YYYY hh:mma"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSString *dateString = [dateFormat stringFromDate:datee];
NSLog(@"date 3 : %@", dateString);

Hope this helps.

tl;dr the Timezone