Convert UTC to local timezone – Node JS

In this tutorial, we will teach you, how you can convert UTC time to your local timezone in Node JS.

Following code will first get the current datetime in UTC, then convert it to user local timezone.

Note: You do not need to provide the timeZone from client side. Node JS will automatically detect the timeZone from incoming request and convert it accordingly.

let date = new Date()

date = new Date(date + " UTC")
date = date.toUTCString()

result.send(date + "")

Most important line here is:

date = new Date(date + " UTC")

If you do not write this line, then you will see the datetime in UTC.

If you want to return the datetime in specific format, you can do it like this:

// Options for date and time formatting
const options = {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: true
}

const date = (new Date()).toLocaleString("en-US", options)

This will return the date and time in format: “Tuesday, 2 July, 2024 at 05:11:05 pm”.

Check this tutorial if you want to convert UTC to local timezone in Python.