I have received date in ISO format fom server-side, like this '2022-09-25T02:00:00Z' and I would like to convert it to another date format like dd-MM-yyyy or yyyy-MM-dd, then how can I do it using Javascript? I would like to know quick and easy solution.
There are multiple ways to convert ISO date format to your required one using Javascript, easily:
var date = new Date("2022-09-25T02:00:00Z");
date.toISOString().substring(0, 10);
//output -> 2022-09-25
var date = new Date('2022-09-25T02:00:00Z');
var year = date.getFullYear(); //get year
var month = date.getMonth()+1; //get month
var dt = date.getDate(); //get date
if (dt < 10) {
dt = '0' + dt; // add leading 0 if date is less than 10
}
if (month < 10) {
month = '0' + month; // add leading 0 if month is less than 10
}
console.log(dt +"-"+ month+"-"+year);
//output: 25-09-2022
https://jsfiddle.net/hm2Lk1u8/
You can also use .toLocalDateString() and pass your required date-format Using locales, for example
let isoDate = "2022-09-25T02:00:00Z";
var date = new Date(isoDate);
date.toLocaleDateString('en-GB'); // dd/mm/yyyy
date.toLocaleDateString('en-US'); // mm/dd/yyyy
date.toLocaleDateString('ko-KR'); // yyyy. mm. dd.
There are many more other formats, here is the sample
cs-CZ: 3. 9. 2022 17:56:58
da-DK: 3.9.2022 17.56.58
de-AT: 3.9.2022, 17:56:58
de-CH: 3.9.2022, 17:56:58
de-DE: 3.9.2022, 17:56:58
el-GR: 3/9/2022, 5:56:58 µ.µ.
en-AU: 03/09/2022, 5:56:58 pm
en-CA: 2022-09-03, 5:56:58 p.m.
en-GB: 03/09/2022, 17:56:58
en-IE: 3/9/2022, 17:56:58
en-IN: 3/9/2022, 5:56:58 pm
en-NZ: 3/09/2022, 5:56:58 pm
en-US: 9/3/2022, 5:56:58 PM
en-ZA: 2022/09/03, 17:56:58
es-AR: 3/9/2022 17:56:58
es-CL: 03-09-2022 17:56:58
es-CO: 3/9/2022, 5:56:58 p. m.
es-ES: 3/9/2022 17:56:58
es-MX: 3/9/2022 17:56:58
es-US: 3/9/2022 5:56:58 p. m.
fi-FI: 3.9.2022 klo 17.56.58
fr-BE: 03/09/2022, 17:56:58
fr-CA: 2022-09-03, 17 h 56 min 58 s
fr-CH: 03.09.2022, 17:56:58
fr-FR: 03/09/2022, 17:56:58
he-IL: 3.9.2022, 17:56:58
hi-IN: 3/9/2022, 5:56:58 pm
hu-HU: 2022. 09. 03. 17:56:58
id-ID: 3/9/2022 17.56.58
it-CH: 3/9/2022, 17:56:58
it-IT: 3/9/2022, 17:56:58
ja-JP: 2022/9/3 17:56:58
ko-KR: 2022. 9. 3. ?? 5:56:58
nl-BE: 3/9/2022 17:56:58
nl-NL: 3-9-2022 17:56:58
no-NO: 3.9.2022, 17:56:58
pl-PL: 3.09.2022, 17:56:58
pt-BR: 03/09/2022 17:56:58
pt-PT: 03/09/2022, 17:56:58
ro-RO: 03.09.2022, 17:56:58
ru-RU: 03.09.2022, 17:56:58
sk-SK: 3. 9. 2022, 17:56:58
sv-SE: 2022-09-03 17:56:58
ta-IN: 3/9/2022, ???????? 5:56:58
ta-LK: 3/9/2022, 17:56:58
th-TH: 3/9/2564 17:56:58
tr-TR: 03.09.2022 17:56:58
zh-CN: 2022/9/3 ?? 5:56:58
zh-HK: 3/9/2022 ?? 5:56:58
zh-TW: 2022/9/3 ?? 5:56:58
ar-SA:
bn-BD:
bn-IN:
Hope it helps.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly