Situation: I want to use the JavaScript function Date.toLocaleDateString() to display a date in the users preferred locale. So far so well, but I want to display month and day with the 2-digit option.
As far as I know, you have to use Date.toLocaleDateString(locale, options) to display with options, but which value should I use for the locale option? Which variable does the toLocaleDateString() read internally to set the locale, so that I can read it out and pass it to the function call with 2 parameters?
From the specification of toLocaleDateString:
If locales is not provided, then let locales be undefined.
Implying you can set it to undefined yourself with no ill effects. This is backed up by the MDN reference documentation:
If the locales argument is not provided or is undefined, the runtime's default locale is used.
So you can call it with:
Date.toLocaleDateString(undefined, options);
to get the default locale as if you'd called it with no arguments.
Related
I'm trying to write a function that takes a timestamp string as an input (same formats as the javascript Date class constructor) and returns the milliseconds since the epoch. If a timezone is not specified in the timestamp UTC should be used as the default. Basically I want the following but with a default timezone:
(input) => new Date(input).valueOf();
The main problem is the multitude of formats. There's too many possible ways to format a date for me to write a timezone parser I can be confident in. There might even be some formats I don't know about.
I tried to utilize the parser already in use in the Date class but if I use the constructor I can't tell whether the resulting timezone is due to it being specified in the timestamp or if it's the default.
new Date('2020-11-11 00:00').valueOf() === new Date('2020-11-11 00:00+02:00').valueOf(); // May be true or false depending on the local timezone
I tried using moment-timezone. It exposes a setDefault method but it doesn't work consistently:
import moment from 'moment-timezone'; // 0.5.27
moment.tz.setDefault('UTC');
moment('2020-11-11').valueOf(); // 1605052800000
moment('2020-11-11 UTC').valueOf(); // 1605052800000
moment('Nov 11 2020').valueOf(); // 1605045600000 <-- expected 1605052800000 instead. Likely affected by local timezone.
moment('Nov 11 2020 UTC').valueOf(); // 1605052800000
If only I had access to whatever parser the Date class uses I could discriminate based on whether a timezone is defined or not but I don't think it's exposed.
How can I use a default timezone without overwriting possible timezone definitions in the timestamps?
Note: I'm using node.js so there's no need to consider browser differences.
I need to show date in localized format, so I used
moment().format('l');
function of Moment JS https://momentjs.com/#multiple-locale-support
It is not working as expected in different systems/browsers
root cause of the issue is that Moment reads current locale as "en" even when the environment is France ("fr")
console.log('moment lang: '+moment.lang());
console.log('userLanguage: '+window.navigator.userLanguage);
console.log('language: '+window.navigator.language);
Results produced:
moment lang: en
userLanguage: fr-FR
language: fr-FR
Moment will give correct locale formatted date if I explicitly set
moment().locale(window.navigator.userLanguage||window.navigator.language);
Do I really need to explicitly set locale in Moment or I am doing something wrong?
A note for duplicate question issue: question is how does Momentjs reads locale.
Yes you have set desired locale using moment.locale:
By default, Moment.js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.js for later use.
To load a locale, pass the key and the string values to moment.locale.
Once you load a locale, it becomes the active locale. To change active locales, simply call moment.locale with the key of a loaded locale.
Please note tha lang is deprecated in 2.8.1
// Deprecated in 2.8.1
moment.lang(String);
I use this code to display a date in French format :
var dateToDisplay = moment(myDateInMS, "x").format("DD/MM/YYYY - hh:mm:ss"); // Output : "20/03/2016 - 12:35:32"
I would like to improve this display to have a better display according the browser language. How can i do that using moment ?
I do not recommend setting the locale depending on the browser language as it's not a clear signal of the actual locale of the user. E.g. a user may use an English operating system even though s/he's a French speaker.
That being said. Reading the language from the browser and setting Moment.js to the corresponding locale can be done this way:
var localeData = moment.localeData();
switch (navigator.language || navigator.userLanguage) {
case 'fr':
localeData = moment.localeData('fr');
break;
}
localeData.longDateFormat('LL'); // the example 'LL' will output date in "D MMMM YYYY format"
Notice that this is setting the locale of the entire Moment.js instance (which is probably what you want to do). Also notice that as of Moment.js 2.8.0, changing the global locale doesn't affect existing instances.
Also see
Changing locale globally
Long Date Format
Moment will actually search for substrings of the locale pulled from the browser in an attempt to pick the correct locale. Thus, there is no need to preparse or create a case statement.
As an example, moment has es as a locale, but not es-mx. If the browser is set to es-mx the locale function looks for es-mx and when it doesn't find it, falls back to es. Thus:
moment.locale('es-mx');
"es"
Meaning that for your purposes you can just do:
moment.locale(navigator.userLanguage || navigator.language);
Then when formatting your dates, use one of the locale specific formats to make them appropriate for the user:
moment().format('LL')
"1 de abril de 2016"
moment().format('L')
"01/04/2016"
For all of the localized formats and what they should produce, see the localized format section of the format documentation: http://momentjs.com/docs/#/displaying/format/
In javascript I'm using Date.toLocaleDateString to format my dates in the user's locale. While in theory it should work, it doesn't.
I am located in the UK. My computer is set to UK and my default language is set to en/gb in both system settings and the browser content settings. Yet, Firefox always displays dates the US format. Is there some trick I'm missing?
The full code for formatting is this:
var timestamp = ...; //some value from ajax call
var dt = new Date(timestamp);
$('#audit-date').text(dt.toLocaleDateString());
In the UK for today's date I would expect to see 05/02/2014, but I see 02/05/2014, which is the US version of it.
Use this to pass the locale.
var locale = window.navigator.userLanguage || window.navigator.language;
alert(date.toLocaleString(locale));
A quick look into to awesome MDN Documentation tells me that you need a locale parameter, otherwise the result depends on the browser. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
// British English uses day-month-year order
alert(date.toLocaleString("en-GB"));
// → "20/12/2012 03:00:00"
For more custom date formats I use the moment.js library. http://momentjs.com/
I have a date string which looks like this: 2013-04-06T14:15:00
I'm looking for functions similar to toLocaleDateString()
(documentation). However, those functions don't take a String parameter; you need to create a Date object first. I'm trying to avoid timezones altogether, so does anyone know of a function (standard or from a plugin) which can format a datestring using a specific locale's rules (1/17/2013 vs 17/1/2013 etc.) using only my datestring?
I'm currently using jQuery, and this plugin for formatting dates: jQuery.dateFormat
Pass your date string to the Date constructor. It parses most legitimate formats.
new Date("1/17/2013")
new Date("2013-04-06T14:15:00")