My date is 2022-01-19T13:00:56.000Z
I need to be able to replace some characters in this, so I need to convert it to a string.
However it loses its format if I use toString():
console.log(myDate.toString())
// output is Wed Jan 19 2022 21:00:56 GM +0800 (Hong Kong Standard Time)
I want to convert the date 2022-01-19T13:00:56.000Z to "2022-01-19T13:00:56.000Z".
How to accomplish this?
toISOString DOES produce a string you can manipulate
const date = new Date("2022-01-19T13:00:56.000Z")
console.log(date.toLocaleString());
// Add 6 hours to the time
date.setHours(date.getHours() + 6);
const isoString = date.toISOString();
// Intermediate result
console.log(isoString);
// Fix the offset
const ZPlus6 = isoString.replace("Z","+0600");
// Adjusted timestamp
console.log(ZPlus6);
// Produces the same date and time as original
console.log(new Date(ZPlus6).toLocaleString());