dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Consider using Intl api

Open benmccann opened this issue 6 years ago • 20 comments

I really like the way Luxon does localization and time zone support by using Intl.DateTimeFormat

For time zones, it allows the user to specify any time zone without any extra files to download compared to solutions for other libraries that are quite large. See their timezone docs and code for more details. This could be a possible method of implementing https://github.com/iamkun/dayjs/issues/323, https://github.com/iamkun/dayjs/issues/249, https://github.com/iamkun/dayjs/issues/46

For localization, it's far easier to maintain because you do not need to create or maintain any translations on your side. This could fix several issues such as https://github.com/iamkun/dayjs/issues/416, https://github.com/iamkun/dayjs/issues/415, and https://github.com/iamkun/dayjs/issues/171. It's also easier for users because they don't need any extra files and everything works out-of-the-box. See their Intl docs and code for more details

Would there be any interest in this approach?

benmccann avatar Feb 15 '19 21:02 benmccann

Good point, but the Internationalisation API isn't supported in all browsers just yet https://caniuse.com/#feat=internationalization

ghost avatar Feb 16 '19 08:02 ghost

There are polyfills that can be used on other browsers.

For localization: <script src="https://cdn.polyfill.io/v2/polyfill.js?features=Intl.~locale.zh"></script>

For time zones: https://github.com/yahoo/date-time-format-timezone

benmccann avatar Feb 16 '19 15:02 benmccann

I think these things should be done by our libs, not leave to our users.

ghost avatar Feb 16 '19 15:02 ghost

Ok, it's good to know that. Though users will have to take some action in either case.

With the Intl api, users that want to support older versions of IE need to do:

<script src="https://cdn.polyfill.io/v2/polyfill.js?features=Intl.~locale.zh"></script>

E.g. with dayjs users have to do this to an even greater extent because it must be done for all browser versions:

<script src="https://unpkg.com/dayjs/locale/zh-cn"></script>

benmccann avatar Feb 16 '19 16:02 benmccann

Good suggestion.

iamkun avatar Feb 18 '19 10:02 iamkun

I just discovered this project and while I was considering to move all my company's projects to this lib I stumbled upon this.

IMHO what's actually killing momentjs is the need to import locales files and the payload burden which this implies. Given the nature of web-apps you cannot know ahead of time (building phase) what locale your users will be needing and importing all of them would result in another momentjs. Leveraging the Intl API provided by the underlying OS/browser is the present and future of date manipulation in JS.

@iamkun would you consider moving to an Intl-first approach with manual locale file importing fallback?

That's all this lib needs to take over in the ecosystem.

I didn't dig into the code to discover how you managed to keep it all under 2kb but hey... GOOD JOB!! 🤟🏻

damianobarbati avatar Mar 04 '19 13:03 damianobarbati

@damianobarbati you could import locale files dynamically based on user's OS.

ghost avatar Mar 04 '19 15:03 ghost

@xxyuk sure, I can async-import anything and work-around whatever in a web application. But the less code, the less bugs. And browser can do the heavy lift, so why carrying this burden? :)

damianobarbati avatar Mar 04 '19 15:03 damianobarbati

date-fns does exactly what @benmccann suggested:

Dependency free IANA time zone support is implemented via the Intl API to keep actual time zone data out of code bundles. Modern browsers all support the necessary features, and for those that don't a polyfill can be used.

dandv avatar Mar 20 '19 03:03 dandv

@issuehunt has funded $200.00 to this issue.


IssueHuntBot avatar May 09 '19 02:05 IssueHuntBot

Intl is a nice choice for timezone plugin

iamkun avatar May 09 '19 02:05 iamkun

the Intl api will output "11th of may" semantically in russian while it remains as a single word and a number, therefore conforming to the order of format that asks for month's day after the month would be incorrect in that language. I'm writing this feature so that it will default to locale file if it's provided, but use and accept intl options otherwise, like localized numbering/calendars while only listening to how abbreviated you want the string to be.

janat08 avatar Jun 01 '19 22:06 janat08

It will also default to the previous behavior with respect to the ordering of date with default locale of en, ignoring intl options.

janat08 avatar Jun 01 '19 22:06 janat08

It is 2020, Intl api is supported by most browser (caniuse) except Opera Mini, QQBrowser and Kalios browser. 95% of the users.

I was looking into this article and Dayjs and Luxon are clearly the winner there. My experience with Luxon and Intl was very good, I'm sad that Day.js does not support it.

HugoGresse avatar Jan 20 '20 17:01 HugoGresse

We're trying to find a simpler way to detect and correct daylight savings related changes, in liu of roling with just the simple solution and rewarding that with the bounty.

janat08 avatar Jan 21 '20 06:01 janat08

Update: Day.js Time Zone Plugin with Intl API

https://day.js.org/docs/en/timezone/timezone

iamkun avatar Aug 04 '20 15:08 iamkun

Update: Day.js Time Zone Plugin with Intl API

https://day.js.org/docs/en/timezone/timezone

Hi @iamkun the timezone plugin works great, however the localizedFormat plugin still depends on loading the locale files

theo-staizen avatar Jan 08 '21 15:01 theo-staizen

Any updates?

vaheqelyan avatar Sep 13 '23 20:09 vaheqelyan

Just to mention, intl isn't updated by browsers too quickly, that's the difference between moment-tz and Intl

Lonli-Lokli avatar Apr 18 '24 22:04 Lonli-Lokli

Any updates?

xdoer avatar Jul 24 '24 09:07 xdoer