Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toLocaleDateString() short format

I want to have the short notation of the Date.toLocaleDateString() but in local format. There are a lot of solutions that hard-code the yyyy-mm-dd format but I want it to be dependent on the system that is hosting the page. This is my function thus far:

    function getDate(dateTimeString)     {         var date    = getDateTime(dateTimeString);         var options = { year: "numeric", month: "numeric", day: "numeric" };                 return date.toLocaleDateString( date.getTimezoneOffset(), options );     } 

but this returns it like so: Wednesday, January 28, 2015 which I don't want. Any suggestions/ideas?

PS: it's not a browser and there is a pretty real possibility that the person using it does not have interwebs connection; all information is gotten from a local database so I can't use any fany stuff like this How to get visitor's location (i.e. country) using javascript geolocation.

like image 490
GeekPeek Avatar asked Jan 14 '15 09:01

GeekPeek


People also ask

How to make short date js?

To convert a full date to a short date in JavaScript, we use the toLocaleDateString method. const date = new Date(). toLocaleDateString("en-US"); to call toLocaleDateString with the locale string to return a string with the short form of the current date.

How to convert date to short date format in JavaScript?

Use the toLocaleDateString() method to convert a full date to a short date. The method takes the locale and an options object as parameters and returns a string that represents the date according to the provided locale and options. Copied! const date = new Date('2022-09-24'); // 👇️ "09/24/2022" console.


2 Answers

I think the function toLocaleDateString use the default local data on the device.

try this code to check the output:

// America/Los_Angeles for the US // US English uses month-day-year order console.log(date.toLocaleDateString('en-US')); // → "12/19/2012"  // British English uses day-month-year order console.log(date.toLocaleDateString('en-GB')); // → "20/12/2012"  // Korean uses year-month-day order console.log(date.toLocaleDateString('ko-KR')); // → "2012. 12. 20."  // Arabic in most Arabic speaking countries uses real Arabic digits console.log(date.toLocaleDateString('ar-EG')); // → "٢٠‏/١٢‏/٢٠١٢"  // chinese console.log(date.toLocaleDateString('zh-Hans-CN')); // → "2012/12/20" 
like image 188
lostomato Avatar answered Oct 05 '22 17:10

lostomato


Note that NodeJS will only ship with the locale format of the device and so when you specify an argument toLocaleDateString like:

new Date("1983-March-25").toLocaleDateString('fr-CA', { year: 'numeric', month: '2-digit', day: '2-digit' }) '03/25/1983' 

Notice you expected 'fr-CA' to give you YYYY-MM-DD, but it did not. That's because it is only ever using the US locale since my Node instance is running in the US locale.

There's actually a bug report on Node github account delineating the issue and solution:

https://github.com/nodejs/node/issues/8500

The solution provided is installing the full-icu module.

like image 28
Daniel Viglione Avatar answered Oct 05 '22 17:10

Daniel Viglione