toLocaleDateString returning String without caring of locale - javascript

I'm developping a Discord bot in NodeJS with Discord.js, and I wanted to parse a date in format "YYYY-MM-DD" and display in long fr-FR format.
I tried :
var dateSortie = new Date("2018-06-03");
var options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
};
console.log(dateSortie.toLocaleDateString("fr-FR", options));
And no matters the locale (fr-Fr, de-De, ...), it's always returning "2018 M06 3, Sun".

Sourcing from this GitHub issue:
By default --with-intl=small-icu is used to build node, which
contains just the en-US locale [...]. You will need to either build
node with --with-intl=full-icu or --with-intl=system-icu if you
want to be able to use more locales. The reason node is built with a
smaller ICU by default is file size.
Besides building your own version of node, an alternative seems to be to install the full-icu module.

Related

How to add specific characters in between values of a JavaScript date?

I am trying to format a date in JavaScript to fit a specific format.
My desired format is 29-Jan-2021
With the below code I have managed to generate "29 Jan 2021":
var newDate = new Date('2021-01-29T12:18:48.6588096Z')
const options = {
year: 'numeric',
month: 'short',
day: 'numeric',
};
console.log(newDate.toLocaleString('en-UK', options))
Can someone please show me how I can add - between the day, month, & year in the date above?
Well you if you have already able to find the string close to your answer you can achieve your solution with either of the two methods.
Either you can use replaceAll() or replace(). Both will be able to solve the issue.
let dateFormat = "29 Jan 2021"
console.log(dateFormat.replaceAll(" ","-")) // 29-Jan-2021
console.log(dateFormat.replace(/ /g,"-")) // 29-Jan-2021
Well I would suggest you to use replace() over replaceAll as some browsers do not support replaceAll(). Do check replaceAll support.

How to convert from date.now to pacific time

On my current project, I have to implement elapsed time for each user and display it on the webpage.
However, using the date.now will return utc and I would like to get it in pacific time. I googled and researched through online but could not get it to work... Is there a way to convert from utc to pacific time?
You can do something like this:
const event = new Date(Date.now());
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(event.toLocaleDateString('us-PT', options));
Where "us" is the "United States" and "PT" is "Pacific-Time"
To see more go to MDN web docs Date.prototype.toLocaleDateString()
I see a few options you have at hand.
Option 1 - use an external library (moment / date-fns)
Consider using these libraries if your app is going to heavily rely on timezones, dates etc.
For example:
moment.tz(12345678, "America/Los_Angeles") // 12345678 being your Date timestamp
See more info here:
date-fns
moment
Option 2 - you need to use vanilla JS
Found a few answers on this thread, but I couldn't see any of them being long term reliable (because of savings time etc.) - I might be wrong about it, but I resort to date/time libraries when my app heavily uses the same.
This worked for me.
const date = new Date().toLocaleDateString('en-US', { timeZone: 'America/Los_Angeles' });
const time = new Date().toLocaleTimeString('en-US', { timeZone: 'America/Los_Angeles' });
const datetime = new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angeles' });
Hope it helps others save time.

Date.toLocaleDateString format issues

The Date.toLocaleDateString() doesn't work in my Windows 10 laptop running nodejs (v10.15.0) as server for a discord.js bot. It shows mm/dd/yyyy instead of dd/mm/yyyy.
I'm using 'en-GB' as the first argument for locale, and second argument for the format I want to achieve (dd/mm/yyyy). And in https://js.do/ , it displays dd/mm/yyyy, but somehow in my laptop it shows as mm/dd/yyyy, and they're both using the same code except for "document.write", I used "console.log" for displaying the result.
let d1 = new Date();
let options = {
year: 'numeric',
month: '2-digit',
day: '2-digit'
};
document.write(d1.toLocaleString('en-GB', options)); // console.log in my laptop
I would expect it to be dd/mm/yyyy format because it's in 'en-GB' locale, instead of the mm/dd/yyyy format.
What is the problem? Is it because of nodejs? or the js.do website? As discussed in this thread: Date.toLocaleDateString() not working on Nodejs v10.14.2 , but I think the issue is slightly different.
Apparently, nodejs by default only contains the en-US locale, as stated here, hence the mm/dd/yyyy format.
I followed the advice by targos in that issue to install the full-icu module.
After installing it, I ran npm install because of this, then I saw this in the command line:
For package.json:
{"scripts":{"start":"node --icu-data-dir=node_modules\\full-icu YOURAPP.js"}}
And edited my start script accordingly, and it produces the desired result of dd/mm/yyyy.
Huge thanks to #quirimmo helping me in the comments of my question !
I was able to recreate this issue, try dateformat if your code allows.
$ npm install dateformat
var dateFormat = require('dateformat');
let d1 = new Date();
console.log(dateFormat(d1, "GMT:dd/mm/yyyy"));

Javascript Date.toLocaleString() ignored in Apigee API Platform

I'm developing a API Proxy in Apigee API Platform. I'm using a Javascript script to convert a Date to a User-Friendly representation.
So I have this code:
var endDate = new Date(2014, 01, 01, 00, 00, 00);
var options = {
localeMatcher: 'best fit',
weekDay: 'short',
year: 'numeric',
month: 'short',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timezone: 'America/El_Salvador',
timeZoneName: 'short'
};
var friendly_endDate = endDate.toLocaleDateString('es-SV', options);
And I always end up with a en-US formatted date like the following:
February 1, 2014 12:00:00 UTC
Timezone, options and locale is ignored. is this normal? Am I missing something?
The latest version of Rhino (1.7R4) was released on 6/18/2012. According to the Mozilla docs on Date.toLocaleDateString, the locales and options arguments were added with the ECMAScript Internalization API, which looks like it came out in December 2012.
I think your best bet is to find a JavaScript library that can be included for your policy, and use that. See the Apigee JavaScript policy documentation for instructions on including JavaScript libraries.
I use moment JS libray. Very nicely design simple library for JS base Date manipulations. Give it a try.
We also have a working sample that shows you how to work with JavaScript libraries as includes in an API proxy:
https://github.com/apigee/api-platform-samples/tree/master/sample-proxies/base64encoder

How to get date in specific locale with JavaScript?

I need to get the date in a specific locale using JavaScript - not necessarily the user's locale. How can I do this?
I did some research and found date.getUTCMonth() and date.getUTCDate(). How can I use this or something else to get the date in a certain locale?
You can use toLocaleDateString:
From the MDN documentation:
The new locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function.
const event = new Date();
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(event.toLocaleDateString('de-DE', options));

Categories