3👍
✅
Here’s a function to convert a string YYYY-MM-DD hh:mm:ss
date time to iso date
function toIsoDate(dateTime){
const date = dateTime.split(" ")[0].split("-");
const time = dateTime.split(" ")[1].split(":");
return new Date(date[2], date[1]-1, date[0], time[0], time[1]);
// or if you want to return ISO format on string
return new Date(date[2], date[1]-1, date[0], time[0], time[1]).toISOString();
}
Date
accept these parameters: (year, month, day, hours, minutes, seconds, milliseconds)
, keep in mind that the month value is 0-11, not 1-12, that’s why we subtract the month (date[1]
) on the function.
const dateTime = "22-07-2020 12:00";
console.log(toIsoDate(dateTime));
2020-07-22T12:00:00.000Z
👤Owl
Source:stackexchange.com