-2

I want to convert 2022-01-15T12:22:24.0078237 into Sat Jan 15 2022 23:24:18 GMT+1100. The answers in Convert UTC date time to local date time say that the Date constructor should convert from UTC to local date, but clearly this isn't working here. What can I do to convert the UTC date into the local client date?

new Date("2022-01-15T12:22:24.0078237")
> Sat Jan 15 2022 12:22:24 GMT+1100 (Australian Eastern Daylight Time)
new Date()
> Sat Jan 15 2022 23:24:18 GMT+1100 (Australian Eastern Daylight Time)
Jay
  • 9
  • 3
  • Isn't your question answered here? https://stackoverflow.com/questions/6525538/convert-utc-date-time-to-local-date-time – Eduard Jan 15 '22 at 12:31
  • @Eduard—the accepted answer there is to add "UTC", which is not a good idea as explained in answers to [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jan 15 '22 at 15:10

1 Answers1

0

Add 'Z' to the date string, like this:

const dateString = "2022-01-15T12:22:24.0078237";
const localDate = new Date(dateString + "Z");
sptm
  • 311
  • 2
  • 9