48

日期格式化使用 YYYY-MM-dd 的潜在问题

 4 years ago
source link: http://blog.didispace.com/something-about-YYYY-MM-dd/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

昨天在v站上看到这个关于YYYY-MM-dd的使用而出现Bug的帖子(v2ex.com/t/633650)非常有意思,所以拿过来分享一下。

在任何编程语言中,对于时间、数字等数据上,都存在很多类似这种平时一切OK,特定时间、特定环境出问题的情况。出现这种问题的根本原因还是我们对于各种数据结构的细节定义在开始的时候都不太注意,都是从每一次使用问题出现之后才开始有了“刻骨铭心”的认识。

不论昨天你是否也经历了这样的Bug,还是没有经历的读者,希望看到这篇文章之后,对这个细节可以上点心,避免日后因为这个细节而犯错。

下面就来一起看看这个问题的表现和原因吧:

问题表现

UBFvAvU.png!web

BUG 的原因

YYYYweek-based-year ,表示:当天所在的周属于的年份,一周从周日开始,周六结束,只要本周跨年,那么这周就算入下一年。所以2019年12月31日那天在这种表述方式下就已经 2020 年了。

而当使用 yyyy 的时候,就还是 2019 年。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK