62

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.

Community
  • 1
  • 1
GeekPeek
  • 1,615
  • 1
  • 20
  • 34
  • What do you mean it is not a browser? – Pochen Jan 14 '15 at 09:43
  • it's a program that can parse HTML markup but it gets it's information from a local database but I can't use things like webservices. – GeekPeek Jan 14 '15 at 09:46
  • 1
    You told us what output you don't want. Can you tell us what output you DO want? There's literally hundreds of different ways to output the same date in just the same language... – John Slegers Feb 26 '16 at 10:22
  • It's kinda funny... I just faced the same problem on ie11 with a local webserver. I just get the en-US formatted long date returned... Tested it on JSFiddle on the same browser and it worked -.- – D4ddy Oct 17 '16 at 20:56

10 Answers10

79

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"
lostomato
  • 970
  • 8
  • 4
  • Very helpful mate. Thanks a lot. – Alok Ranjan Aug 19 '20 at 16:18
  • If you need order dates you will need to pass options object, because with localeDateString in dates for example 7/7/2011 will be after of 20/7/2011 for example. options : { year : 'numeric', month : '2-digit', day : '2-digit' } – inane Jul 17 '21 at 14:58
57

JavaScript Date toLocaleString() This method formats a date into a string, using language specific format.

Examples :

Only date :

var n = new Date();
console.log("es-CL: " + n.toLocaleDateString("es-CL"));
// es-CL: 03-09-2021

Date with Time :

var n = new Date();
console.log("es-CL: " + n.toLocaleString("es-CL"));
// es-CL: 03-09-2021 17:56:58

List are here :

ar-SA: ٢٦‏/١‏/١٤٤٣ هـ في ٥:٥٦:٥٨ م
bn-BD: ৩/৯/২০২১ ৫:৫৬:৫৮ PM
bn-IN: ৩/৯/২০২১ ৫:৫৬:৫৮ PM
cs-CZ: 3. 9. 2021 17:56:58
da-DK: 3.9.2021 17.56.58
de-AT: 3.9.2021, 17:56:58
de-CH: 3.9.2021, 17:56:58
de-DE: 3.9.2021, 17:56:58
el-GR: 3/9/2021, 5:56:58 μ.μ.
en-AU: 03/09/2021, 5:56:58 pm
en-CA: 2021-09-03, 5:56:58 p.m.
en-GB: 03/09/2021, 17:56:58
en-IE: 3/9/2021, 17:56:58
en-IN: 3/9/2021, 5:56:58 pm
en-NZ: 3/09/2021, 5:56:58 pm
en-US: 9/3/2021, 5:56:58 PM
en-ZA: 2021/09/03, 17:56:58
es-AR: 3/9/2021 17:56:58
es-CL: 03-09-2021 17:56:58
es-CO: 3/9/2021, 5:56:58 p. m.
es-ES: 3/9/2021 17:56:58
es-MX: 3/9/2021 17:56:58
es-US: 3/9/2021 5:56:58 p. m.
fi-FI: 3.9.2021 klo 17.56.58
fr-BE: 03/09/2021, 17:56:58
fr-CA: 2021-09-03, 17 h 56 min 58 s
fr-CH: 03.09.2021, 17:56:58
fr-FR: 03/09/2021, 17:56:58
he-IL: 3.9.2021, 17:56:58
hi-IN: 3/9/2021, 5:56:58 pm
hu-HU: 2021. 09. 03. 17:56:58
id-ID: 3/9/2021 17.56.58
it-CH: 3/9/2021, 17:56:58
it-IT: 3/9/2021, 17:56:58
ja-JP: 2021/9/3 17:56:58
ko-KR: 2021. 9. 3. 오후 5:56:58
nl-BE: 3/9/2021 17:56:58
nl-NL: 3-9-2021 17:56:58
no-NO: 3.9.2021, 17:56:58
pl-PL: 3.09.2021, 17:56:58
pt-BR: 03/09/2021 17:56:58
pt-PT: 03/09/2021, 17:56:58
ro-RO: 03.09.2021, 17:56:58
ru-RU: 03.09.2021, 17:56:58
sk-SK: 3. 9. 2021, 17:56:58
sv-SE: 2021-09-03 17:56:58
ta-IN: 3/9/2021, பிற்பகல் 5:56:58
ta-LK: 3/9/2021, 17:56:58
th-TH: 3/9/2564 17:56:58
tr-TR: 03.09.2021 17:56:58
zh-CN: 2021/9/3 下午5:56:58
zh-HK: 3/9/2021 下午5:56:58
zh-TW: 2021/9/3 下午5:56:58
Michal Skop
  • 1,349
  • 1
  • 15
  • 23
32

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.

Daniel Viglione
  • 8,014
  • 9
  • 67
  • 101
18

You could try something like:

var date = new Date(Date.UTC(2015, 0, 28, 4, 0, 0));
console.log(date.toLocaleDateString("nl",{year:"2-digit",month:"2-digit", day:"2-digit"}));

Which gives me "28-01-15" in Chrome (48.0.2564.116) at least.

Firefox just returns "01/28/2015", and phantomJS returns "28/01/2015" regardless of the locale.

Nigel Scott
  • 643
  • 6
  • 12
14

Yes. Its pretty straightforward. You can use the date object as follows:

var d = new Date();
var mm = d.getMonth() + 1;
var dd = d.getDate();
var yy = d.getFullYear();

Then you should have the numbers you need to form a string in whatever format you need.

var myDateString = yy + '-' + mm + '-' + dd; //(US)

Note this will give something like 2015-1-2 if the digits are in single figures, if you need 2015-01-02 then you will need further converting.

Also please note this will only give the 'client' date, ie. the date on the users system. This should be in their local time. If you need server time then you will have to have some kind of api to call.

dSkech
  • 339
  • 1
  • 5
  • 3
    but then I still end up with a hard-coded format or is there a way to format it for me? (for example: US has yyyy-mm-dd, most of Europe has dd-mm-yyyy etc.) – GeekPeek Jan 14 '15 at 09:47
5

For date : .toLocaleDateString('en-US', {day: "numeric"})

For month in full : .toLocaleDateString('en-US', {month: "long"})

For month in short : .toLocaleDateString('en-US', {month: "short"})

For day in full : .toLocaleDateString('en-US', {day: "long"})

For dayin short: .toLocaleDateString('en-US', {day: "short"})

Rahul Sarma
  • 763
  • 2
  • 8
  • 17
3
 const options = { year: 'numeric', month: 'numeric', day: 'numeric' }
 const new Date(date).toLocaleDateString('en', options)
Gerd
  • 89
  • 4
  • 1
    What value does this answer add? It seems all this has been mentioned above already. – idmean Oct 12 '21 at 06:47
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Oct 12 '21 at 14:08
1

Apparently Date.prototype.toLocaleDateString() is inconsistent across browsers. You can implement different variations of a short date format, as explained here: How format JavaScript Date with regard to the browser culture?

Community
  • 1
  • 1
kmandov
  • 3,130
  • 16
  • 15
  • 3
    I assume browsers differences apply only when a locale and/or other options are not explicitly set for `toLocaleDateString`?! Shouldn't output of eg. `str.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })` be consistent across browsers?! – John Slegers Feb 26 '16 at 10:20
1

var countries = [
        "ar-SA",
        "bn-BD",
        "bn-IN",
        "cs-CZ",
        "da-DK",
        "de-AT",
        "de-CH",
        "de-DE",
        "el-GR",
        "en-AU",
        "en-CA",
        "en-GB",
        "en-IE",
        "en-IN",
        "en-NZ",
        "en-US",
        "en-ZA",
        "es-AR",
        "es-CL",
        "es-CO",
        "es-ES",
        "es-MX",
        "es-US",
        "fi-FI",
        "fr-BE",
        "fr-CA",
        "fr-CH",
        "fr-FR",
        "he-IL",
        "hi-IN",
        "hu-HU",
        "id-ID",
        "it-CH",
        "it-IT",
        "ja-JP",
        "ko-KR",
        "nl-BE",
        "nl-NL",
        "no-NO",
        "pl-PL",
        "pt-BR",
        "pt-PT",
        "ro-RO",
        "ru-RU",
        "sk-SK",
        "sv-SE",
        "ta-IN",
        "ta-LK",
        "th-TH",
        "tr-TR",
        "zh-CN",
        "zh-HK",
        "zh-TW"
      ];

      countries.forEach( (country) => {
        console.log( `${country}: ${new Date().toLocaleString(country)}` );
      } );
antelove
  • 3,216
  • 26
  • 20
0

For this format YYYY-MM-DD, use this

var mm = ("0" + (date.getMonth() + 1)).slice(-2);
var dd =("0" + (date.getDate())).slice(-2);
var yy = date.getFullYear();
var dateString = yy + '-' + mm + '-' + dd;

Example of the result
2023-08-21