Search code examples
javascriptdatedatetimetimeutc

How to convert UTC to local date in JavaScript


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)

Solution

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

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