5 ms·
TIL: Intl.DateTimeFormat [1] has good browser support [2] and allows for nifty tricks like > new Intl.DateTimeFormat('zh', { hour: 'numeric' }).format(new Da
by divbzero 6y ago
TIL: Intl.DateTimeFormat [1] has good browser support [2] and allows for nifty tricks like
> new Intl.DateTimeFormat('zh', { hour: 'numeric' }).format(new Date)
< "下午11时"
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... "MDN: Intl.DateTimeFormat"
[2]: https://caniuse.com/mdn-javascript_builtins_intl_datetimeformat_format https://caniuse.com/mdn-javascript_builtins_intl_datetimefor... "Can I Use: Intl.DateTimeFormat"
- Semaphor 6y agoThe whole Intl namespace is a godsend.
- Ambroos 6y agoUntil you start seeing times like 24:45 in your app. The spec has some really strange behaviour in it that defaults to "1-24"-hour time instead of the common "0-23" if you specify hour12: false. Only Chrome seems to have actually implemented the spec this strictly, they don't want to change it: https://bugs.chromium.org/p/chromium/issues/detail?id=1045791 https://bugs.chromium.org/p/chromium/issues/detail?id=104579...
- sebazzz 6y agoIt isn't the pure javascript feeling without a few cross-browser quirks.
- ehsankia 6y agoNot sure if this is a stupid suggestion, but could there be a drop-in replacement for moment which uses Intl and other modern browser APIs, otherwise fetches the full moment.js? Or does MomentJS already do that?
- deleted 6y ago[deleted]