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"
Answer from lostomato on Stack OverflowI 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"
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
sharepoint online - How to format toLocaleDateString([$MyDateTimeColumn]) to return YYYY-MM-DD? - SharePoint Stack Exchange
Jquery new Date - Convert to yyyy-MM-dd and use toLocaleDateString
How do I get the date format string like YYYY-MM-DD for the current locale?
How do I format a date in JavaScript? - Stack Overflow
Try using fr-CA that returns the format in yyyy-MM-dd.
var yyyymmdd = (new Date ()).toLocaleDateString ("fr-CA");
console.log (yyyymmdd);
If you need the local date (GMT+10 in your case), you need to use methods of Date:
function toHtmlDate (d) {
return (d.getFullYear() + '-0' + (d.getMonth() + 1) + '-0' + d.getDate()).replace(/-0(\d\d)/g, '-$1');
}
If you need an ISO date (GMT), you can use new Date().toISOString().substring(0, 10). For explanation:
Date.prototype.toLocaleDateStringoutput depends on your locale, using string splitting on it might not work for another userDate.prototype.toISOStringalways returns####-##-##T##:##:##.###<timezone>, for example2015-10-18T23:23:22.880Z. Take the first 10 characters, you have<year>-<month>-<date>.
Hi, as the title says, I'm not trying to format a date, but I'm trying to get the format string that would be used to format the date. date.toLocaleDateString("de-DE") Would format today's date as 7.9.2022, but I would like the formatting string for any locale instead. I already looked into the docs for Firefox which just said, that it uses the OS's preferences to format each locale, but their was no mention on how to retrieve those preferences.
Does anyone know how to do that?
Thanks in advance for your help.
For any one looking for a really simple ES6 solution to copy, paste and adopt:
const dateToString = d => `${d.getFullYear()}-${('00' + (d.getMonth() + 1)).slice(-2)}-${('00' + d.getDate()).slice(-2)}`
// how to use:
const myDate = new Date(Date.parse('04 Dec 1995 00:12:00 GMT'))
console.log(dateToString(myDate)) // 1995-12-04
The JavaScript Intl.DateTimeFormat method provides a convenient way to format dates.
Here is how the needed format can be constructed:
const date = new Date("2010-08-10");
let d=new Intl.DateTimeFormat('en-GB',{year:"numeric", month:"short",day:"2-digit"}).format(date).split(" ").join("-");
console.log(d);

