NSDateFormatter中'YYYY'和'yyyy'之间的区别 [英] Difference between 'YYYY' and 'yyyy' in NSDateFormatter
问题描述
'YYYY'和'yyyy'之间的确切区别是什么?我在中读到这个链接,它表明
What is exact difference between 'YYYY' and 'yyyy'. I read in this link, it states that
常见的错误是使用YYYY。 yyyy指定日历年
而YYYY指定年份(年度周),用于ISO
年周历。在大多数情况下,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.
但是当我尝试使用
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
在此处,结果为日期
和 datee
不同,我理解,但为什么日期2和日期3的结果不同?当我从字符串创建日期并再次将其反转为字符串,但输出不匹配?
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.
谢谢..
编辑: -
如果我编码
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
,检查NSLog的日期2,奇怪的结果,为什么?
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是基于周的日历年。
@"YYYY" is week-based calendar year.
@yyyy是普通的日历年。
@"yyyy" is ordinary calendar year.
你可以通过整个博客,很好看看它
You can go through the whole blog, its a good to give it a look
这篇关于NSDateFormatter中'YYYY'和'yyyy'之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!