Code Premix

Convert datetime to another time zone using Moment JS

📅August 2, 2021

Today we’ll show you how to convert datetime to another time zone using Moment JS. Additionally, we’ll show you more conversion functions such as convert local time to UTC time, UTC time to desire time zone, 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.

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data-2012-2022.min.js"></script>

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.

// get current local date and time
function getCurrentLocalDateTime() {
  return moment().format('YYYY-MM-DD hh:mm:ss A');
}
// Current local date:
getCurrentLocalDateTime(); // Output: 2021-07-20 06:11:47 PM

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.

// get current UTC date and time
function getCurrentUTCDateTime() {
  return moment.utc().format('YYYY-MM-DD hh:mm:ss A');
}
// Current UTC date:
getCurrentUTCDateTime(); // Output: 2021-07-20 12:41:47 PM

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.

// convert local time to UTC time
function convertLocalToUTC(dt, dtFormat) {
  return moment(dt, dtFormat).utc().format('YYYY-MM-DD hh:mm:ss A');
}
// Current local date to UTC date:
convertLocalToUTC(moment()); // Output: 2021-07-20 12:41:47 PM

// Local date "2021-07-20 10:12:44 PM" to UTC date:
convertLocalToUTC('2021-07-20 10:12:44 PM'); // Output: 2021-07-20 04:42:44 PM

// Local date "20-07-2021 10:12:44 PM" to UTC date:
convertLocalToUTC('20-07-2021 10:12:44 PM', 'DD-MM-YYYY hh:mm:ss A'); // Output: 2021-07-20 04:42:44 PM

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.

// convert utc time to local time
function convertUTCToLocal(utcDt, utcDtFormat) {
  var toDt = moment.utc(utcDt, utcDtFormat).toDate();
  return moment(toDt).format('YYYY-MM-DD hh:mm:ss A');
}
// UTC date "2021-07-20 04:42:44 PM" to local date:
convertUTCToLocal('2021-07-20 04:42:44 PM'); // Output: 2021-07-20 10:12:44 PM

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.

// convert utc time to another timezone
function convertUTCToTimezone(utcDt, utcDtFormat, timezone) {
  return moment.utc(utcDt, utcDtFormat).tz(timezone).format('YYYY-MM-DD hh:mm:ss A');
}
// UTC date "2021-07-20 04:42:44 PM" to "America/Los_Angeles" timezone date:
convertUTCToTimezone('2021-07-20 04:42:44 PM', null, 'America/Los_Angeles'); // Output: 2021-07-20 09:42:44 AM

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.

// convert local time to another timezone
function convertLocalToTimezone(localDt, localDtFormat, timezone) {
  return moment(localDt, localDtFormat).tz(timezone).format('YYYY-MM-DD hh:mm:ss A');
}
// Local date "2021-07-20 10:12:44 PM" to "America/Los_Angeles" timezone date:
convertLocalToTimezone('2021-07-20 10:12:44 PM', null, 'America/Los_Angeles'); // Output: 2021-07-20 09:42:44 AM

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

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