gpt4 book ai didi

ios - NSDateFormatter 中 'YYYY' 和 'yyyy' 之间的区别

转载 作者:IT老高 更新时间:2023-10-28 11:29:50 31 4
gpt4 key购买 nike

“YYYY”和“yyyy”之间的确切区别是什么。我读过 this 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.

但是当我尝试使用时

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,检查 NSLog 的日期 2,奇怪的结果,为什么?

最佳答案

在使用日期格式字符串时,使用正确的格式也很重要。

@"YYYY"是基于周的日历年。

@"yyyy"是普通日历年。

你可以浏览整个博客,看看就好了

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 (死链接)

关于ios - NSDateFormatter 中 'YYYY' 和 'yyyy' 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15133549/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com