I want to use Intl.DateTimeFormat
to format a Date, and in the examples it says
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
Great, so I want my fallback to be ISO 8601 in the case a language doesn't exist
// i.e. the same as/similar to
new Date().toISOString(); // "2014-07-31T02:42:06.702Z"
however
// Intl.DateTimeFormat([locales [, options]])
var o = {};
o.year = o.month = o.day = o.hour = o.minute = o.second = 'numeric';
new Intl.DateTimeFormat(['foo', 'iso8601'], o);
// RangeError: Invalid language tag: iso8601
This seems to be because iso8601
isn't part of
locales
A string with a BCP 47 language tag, or an array of such strings.
I've also tried using one I know works, e.g. en-GB
with a u-ca-iso8601
suffix but this doesn't produce any different result to without the suffix
var f = new Intl.DateTimeFormat(['foo', 'en-GB-u-ca-iso8601'], o);
f.format(new Date());
// 31/7/2014 03:35:26
Why isn't this working? Is there even a locale
which will give me the output I'm looking for?
I'd rather not have to write some complicated wrapper using e.g.
if (Intl.DateTimeFormat.supportedLocalesOf(['foo']).length === 0)
The Intl. DateTimeFormat object is a constructor for objects that enable language sensitive date and time formatting.
ISO 8601 is an international standard covering the worldwide exchange and communication of date and time-related data. It is maintained by the Geneva-based International Organization for Standardization (ISO) and was first published in 1988, with updates in 1991, 2000, 2004, and 2019.
They're for different purposes. UTC is the primary time standard by which the world regulates clocks and time. ISO is standard format time.
Years later I find that the accepted answer which is based on the 'fo' or 'foo' (Faroese) locale no longer works. Perhaps it is as Jukka speculated: the Faroese authorities decided to switch (to d/m/y) or their locale info got corrected.
And, unfortunately, the explicit 'ISO8601' locale that is proposed has still not been added so that is a long way off if it ever happens.
Regarding the comment by @felixbuenemann, I'm a little puzzled as Node 10 does not support locale data and just gives "en-us" results for all (perhaps it was a custom build of Node).
But I concur that the Canadian locales ('fr-CA' and 'en-CA') are good substitutes - the point is to use a locale such as Canada or Sweden where ISO 8601 has been adopted so the locale format is very unlikely to change. Sweden (sv-SE) is actually better than Canada because it doesn't add a non-standard comma and uses more conformant timezone identifiers.
So, what I'm suggesting is
new Date().toLocaleString( 'sv-SE', o );
// where the options 'o' is defined as in the question or however you want
which produces "2019-09-03 05:29:54". Note that the space delimiter instead of 'T' is standard conformant too. Don't append the 'Z' at the end (as in the question) unless you are overriding local time by setting the timeZone option to GMT.
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