Hi
I retrieve a time from an API, and I want to display it with the french format.
I use the following code in backend function:
let targetDeliveryDateString = new Date(targetDeliveryTime).toLocaleString(“fr-FR”, { timeZone:“Europe/Paris”, hour12: false });
Expected result (for the 22nd of April 2019, 5:50:51PM):
22/04/2019, 17:50:51
Actuel result:
4/22/2019, 17:50:51
I have tested with other locales (en-GB for instance) but the date is never properly formatted.
Any help appreciated,
Cheers,
Jérôme
Firstly, check what your own settings are on in your Wix Editor.
https://support.wix.com/en/article/changing-your-sites-regional-settings
Have a read of these pages:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
https://support.wix.com/en/article/corvid-formatting-dates
https://www.wix.com/corvid/forum/community-discussion/working-with-dates-and-time-in-wix-code
https://stackoverflow.com/questions/85116/display-date-time-in-users-locale-format-and-time-offset
https://www.wix.com/release/notes/article/385e0230-dd72-4c35-8965-aaf863d9c85f?utm_source=email_mkt&utm_campaign=em_marketing_whats_new_01032017_en&experiment_id=text_link_2
Also, as stated on those pages:
Avoid comparing formatted date values to static values
Most of the time, the formatting returned by toLocaleString() is consistent. However, this might change in the future and isn’t guaranteed for all languages — output variations are by design and allowed by the specification. Most notably, the IE and Edge browsers insert bidirectional control characters around dates, so the output text will flow properly when concatenated with other text.
For this reason you cannot expect to be able to compare the results of toLocaleString() to a static value:
"1.1.2019, 01:00:00" === new Date("2019-01-01T00:00:00.000000Z").toLocaleString();
// true in Firefox and others
// false in IE and Edge
Hi,
Thanks a lot for detailed and answer.
Worked great by saving in a Date & Time field, and using the latest link you provided.
Much appreciated,
Jérôme