Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ISO 8601 using Intl.DateTimeFormat

Tags:

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)
like image 634
Paul S. Avatar asked Jul 31 '14 02:07

Paul S.


People also ask

What is Intl DateTimeFormat?

The Intl. DateTimeFormat object is a constructor for objects that enable language sensitive date and time formatting.

What is the international ISO 8601?

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.

Is ISO 8601 same as UTC?

They're for different purposes. UTC is the primary time standard by which the world regulates clocks and time. ISO is standard format time.


1 Answers

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.

like image 181
Tom Avatar answered Sep 20 '22 20:09

Tom