

Unicode tokens in date-fns v2
source link: https://www.tuicool.com/articles/hit/3IZfYfR
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.

In this post, I tell about how and why we switched from Moment.js formatting tokens to Unicode Technical Standard #35: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
> date-fns v2 introduces many new features and breaking changes and this post is a part of a series #date_fns_v2 where we describe in detail most notable ones.
When we worked on the format function, we mimicked Moment.js behavior that we thought would simplify the transition to date-fns. It also didn't make sense to reinvent the wheel, so we decided to stick to its selection of tokens.
In turned out that YYYY and DD (year and day) tokens are misused in Moment.js because many other languages (Java, C#, Objective-C, etc.) use yyyy and dd for this purpose:
In v2 we decided to improve on consistency and standardization. We worked hard to make date-fns behave like ECMAScript in edge cases and opted to use existing standards over common conventions. So it made sense to follow the proposed specifications from the Unicode Consortium.
Unfortunately, this change caused a flood of issues reporting misbehaving format and parse . Developers kept using YYYY and DD and getting confusing results. To solve this problem, we had to disable confusing tokens D , DD , YY and YYYY and throw an error:
format(Date.now(), 'YYYY-MM-DD') //=> RangeError: `options.awareOfUnicodeTokens` must be set to `true` to use `YYYY` token; see: https://git.io/fxCyr
To enable those tokens, you should set the awareOfUnicodeTokens option:
format(Date.now(), 'YY-MM-dd', { awareOfUnicodeTokens: true }) //=> '86-04-04'
I hope in the future when and if developers get used to the standard we'll remove this option.
・・・
Happy coding time! Please support us at Open Collective: opencollective.com/date-fns
Recommend
-
190
This add-on defines three company-mode backends: company-math-symbols-latex - math latex tags (by default, active only on latex math faces...
-
200
Slug Generator Library This library provides methods to generate slugs for URLs, filenames or any other target that has a limited character set. It’s based on PHPs Transliterator class which uses the data of the
-
131
latex-unicoder.vim A plugin to type Unicode chars in Vim, using their LaTeX names. This is a more extensive version of rtdavis22/latex2unicode. I obtained the...
-
86
Proposed Update Not Available
-
109
404 | 百度EUX 页面搬家啦,点击此处跳转3 s 后将自动跳转...
-
105
使用技巧事实上,只要遵守以下规则,可以规避90%由于Unicode字符串处理引起的bug,剩下的10%通过python的库和模块能够解决。程序中出现字符串时一定要加个前缀u。不要用str()函数,用unicode()代替。不要用过时的string模块——如果传给它的是非ASCII字符,它会把一...
-
113
Emoji 11.0 data has been released, with 157 new emoji such as: hot face woozy face woman, red haired: light s...
-
2
Date-fns vs MomentJS: Choosing the Right Date Utility LibraryComparing popular JavaScript date libraries.MomentJS is one of the most used JavaScript date li...
-
10
把moment.js换成了date-fns , 打包输出减少了50%+发布于 8 月 22 日date-fns官方网站:
-
14
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK