Convert the local time to another timezone using JavaScript

While you are working with date time in project then you might need to convert the local time to another timezone using JavaScript or you need to convert a UTC time to another timezone. So today we will show you how to convert the local time to another timezone using JavaScript.

Convert date to another timezone, convert javascript date to a particular timezone, initialize a JavaScript Date to a particular time zone, Convert UTC date time to local date time, Handling Time Zone in JavaScript, Automatic timezone conversion, JavaScript getTimezoneOffset() Method, Convert JavaScript Local Date to UTC, Work with Date and Time in JavaScript.

Follow the below steps to convert local time to another timezone

  1. Get current local time in milliseconds
  2. Find the local timezone offset
  3. Obtain the current UTC time
  4. Get destination city’s UTC offset in hours
  5. Get date of another timezone in readable datetime

1. Get current local time in milliseconds

Using a new Date() object we can get the current date time in the local timezone and we have to convert it into the milliseconds.

2. Find the local timezone offset

Using getTimezoneOffset() method we can find the local timezone offset. By default this method returns the timezone offset in minutes, so let’s convert this value into milliseconds for further process.

3. Obtain the current UTC time

To get the current UTC time, we have to add the local timezone offset to the local time.

4. Get destination city’s UTC offset in hours

Move forward to get the destination city’s UTC offset, first get destination city’s offset (For example, we are considering the destination city as Ahmedabad which is 5.5 hours). Now convert it to the milliseconds and add it to the UTC time.

5. Get date of another timezone in readable datetime

Now, we can get the date of another timezone in readable format using new Date() object and call the toLocaleString() method.

Example:

Let’s combine all the steps together and create function which can return the local time of the destination city by passing the offset of destination city as parameter.

That’s it for today.

Thank you for reading and happy coding!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

2 Responses

  1. Date says:

    We are considering a fixed offset. Will not work for timezones having Day Light Saving.

Leave a Reply

Your email address will not be published. Required fields are marked *