ios NSDateFormatter 中“YYYY”和“yyyy”的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15133549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between 'YYYY' and 'yyyy' in NSDateFormatter
提问by P.J
What is exact difference between 'YYYY' and 'yyyy'. I read in this link, it states that
'YYYY' 和 'yyyy' 之间的确切区别是什么。我在这个链接中读到,它指出
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 指定日历年,而 YYYY 指定年份(“一年中的一周”),用于 ISO 年-周日历。在大多数情况下,yyyy 和 YYYY 产生相同的数字,但它们可能不同。通常,您应该使用日历年。
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?
在这里,结果为date
和datee
不同,我理解,但为什么日期 2 和日期 3 的结果不同?当我从字符串创建日期并再次将其反转为字符串时,但输出不匹配?
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?
如果我得到正确的结果,但我作为字符串传递给我得到的结果相同2013-01-05 12:00:00 +0000
,请检查 NSLog 的日期 2,奇怪的结果,为什么?
采纳答案by X-Factor
Also when using a date format string using the correct format is important.
此外,使用正确格式的日期格式字符串也很重要。
@"YYYY" is week-based calendar year.
@"YYYY" 是基于周的日历年。
@"yyyy" is ordinary calendar year.
@"yyyy" 是普通日历年。
You can go through the whole blog, its a good to give it a look
可以翻遍整个博客,看看就好了
http://realmacsoftware.com/blog/working-with-date-and-time(dead link)
http://realmacsoftware.com/blog/working-with-date-and-time(死链接)
回答by Gardner Bickford
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 指定日历年,而 YYYY 指定年份(“一年中的一周”),用于 ISO 年-周日历。在大多数情况下,yyyy 和 YYYY 产生相同的数字,但它们可能不同。通常,您应该使用日历年。
from Apple Docs
来自苹果文档
回答by gowthaman-mallow
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.
回答by CoderPug
All answers differentiating yyyy
and YYYY
are right answers for another question. The question itself refers to another thing.
所有答案都有区别,yyyy
并且YYYY
是另一个问题的正确答案。问题本身是指另一件事。
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.
@PJ 的答案是它们的价值并没有真正不同。当您记录 NSDate (即日期 2)时,您将获得恰好位于 UTC Timezone 的对象的完整描述。记录日期 3 时不会发生此逻辑,因为它已转换为字符串并应用您的时区。
For printing Date 3 the 'same way' as you are getting Date 2. You should specify UTC TimeZone for Date 3. Something like this :
要以与获取日期 2 的“相同方式”打印日期 3。您应该为日期 3 指定 UTC 时区。如下所示:
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
tl;博士时区