As you know ICU supports the following calendars:
Japanese
Buddhist
Chinese
Persian
Indian
Islamic
Hebrew
Indian
Coptic
Ethiopic
However IntlDateFormatter
only convert the Gregorian format of date to a locale and does not convert the calendar automatically, yet it is possible to send to it the calendar and it will do the trick. The problem is I have to figure out the calendar system manually for each locale.
How to get the calendar type from locale string. for example sending fa_IR
I need to get 'persian', so send it to IntleDateFormatter to generate a data string for that locale.
Using toLocaleString () In basic use without specifying a locale, a formatted string in the default locale and with default options is returned. let date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0)); console.log(date.toLocaleString());
Inclusion of a Locale is fairly simple, for the default locale you have the following options that can then be applied to the format/parse options demonstrated above: The 'dateStyle' above is a FormatStyle option Enum to represent the full, long, medium and short versions of the localized Date when working with the DateTimeFormatter.
This behavior is designed to support debugging and to be compatible with previous uses of toString that expected language, country, and variant fields only. To represent a Locale as a String for interchange purposes, use toLanguageTag () .
The method has two input arguments. First is a string called “locale” and the second is an object called “options” that holds individual settings. Return value of the method is of course Date string formatted according to provided language-specific conventions. // 17. 7.
In short; you can't. Why?
Let's take a look at what both objects are:
IntlDateFormatter
is part of the Internationalization extension built for performing various locale-aware operations. Amongst these operations are Date formatters and a locale-aware calendar.
Locale class
is an identifier used to get language, culture or regionally-specific behaviour.
Even though they are part of the same extension, they are orthogonal as described here. That means that you cannot determine one from the other. This statement is further proven in the user guide provided by the ICU project.
... At present, all locales default to a Gregorian calendar, except for the compatibility locales th_TH_TRADITIONAL and ja_JP_TRADITIONAL. If the "calendar" keyword is supplied, this value will override the default for that locale.
Source: User guide ICU project
As the quote states any locale defaults to the Gregorian calendar unless the "calendar" keyword is provided. They cannot derive the calendar from the locale provided automatically.
The solution to your problem is most likely found in building a wrapper yourself. There are some packages on the internet that help you with this but it's nowhere near what you exactly want. If you need to save the date, save it as a timestamp and show it in the format desired by the user, the latter being if you have the freedom to implement some account settings or anything alike.
One module where I found inspiration in my quest for the answer is GlobalizeJS. There's an open open issue that paints an interesting picture of how difficult it is to get right. Take for example the United Arab Emirates (AE), they have five calendar preferences; Gregorian, Islamic Um al-Qura, Islamic, Islamic Civil and Islamic Tabular. I would let the user determine which calendar they prefer so it's most usable for them.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With