Convert local time to another timezone using Moment JS

Today we’ll show you how to convert local time to another timezone using Moment JS. Additionally, we’ll show you more conversion functions such as convert local time to UTC time, UTC time to desire timezone, etc.

Previously, we have covered the article where you can get the local time to another timezone but it’s in JavaScript. Here we will use the Moment.js.

Use moment and moment-timezone script

We have to add the following moment scripts to convert time.

Convert time using Moment.js

  1. Get current local date and time
  2. Get current UTC date and time
  3. Convert local time to UTC time
  4. Convert UTC time to local time
  5. Convert UTC time to another timezone
  6. Convert local time to another timezone

1. Get current local date and time

The below function will give you the local date and time using moment.js.

You can change the format of the return date. Check out the format of the date.

2. Get current UTC date and time

If you want to get an UTC datetime then use the following function.

3. Convert local time to UTC time

Moment is providing the .utc() function to get UTC datetime that function will be used to return UTC time. To convert local time to UTC time, run the below function.

In the above function, we consider two different parameters, one is date and second one is format that is used to pass the format of the given date.

4. Convert UTC time to local time

Now, let’s convert an UTC time to local time for that use the following function.

In the above code, first we have converted the UTC datetime to the native date object then return the formatted date.

5. Convert UTC time to another timezone

Let’s create one more function to convert an UTC time to another timezone.

We need to pass the UTC datetime along with the timezone. We have taken the America/Los_Angeles timezone for an example.

6. Convert local time to another timezone

At the last, we will convert local time to another timezone.

Same as the previous function, we used the .tz() method of the moment.

That’s it for today.
Thank you for reading. Happy Coding..!!

You may also like...

Leave a Reply

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