How to convert iso date to date-format JavaScript?


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.


Asked by:- jon
0
: 2072 At:- 9/26/2022 8:00:13 AM
Javascript ISO date to date-format







1 Answers
profileImage Answered by:- vikas_jk

There are multiple ways to convert ISO date format to your required one using Javascript, easily:

Using substring() extract only date (yyyy-MM-dd)

var date = new Date("2022-09-25T02:00:00Z");
date.toISOString().substring(0, 10);

//output -> 2022-09-25

Get date object and format (dd-MM-YYYY)

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/

Using toLocalDateString()

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.

1
At:- 9/26/2022 8:21:10 AM
Thanks for excellent answer, I like substring method which was very easy. 0
By : jon - at :- 9/26/2022 8:26:35 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use