How to convert UTC date time into local date time using JavaScript?


Timezone handling is an essential component of every web application. The time that is recorded in the backend is usually in UTC format. When it is displayed to the user, however, it must be converted to the user's local time. This is possible with JavaScript. We'll look at how to use JavaScript to convert UTC date time to local date time in this blog.

JavaScript Date

JavaScript includes a "Date" class that allows us to work with dates and timings. The Date class includes various methods for working with dates and times, including −

Date() - returns the current date and time in milliseconds getTime() returns the current time in milliseconds

getUTCFullYear() - returns a date's year in the UTC time zone.

getUTCMonth() - returns a date's month in the UTC time zone.

getUTCDate() - returns the month and day of a date in the UTC time zone.

getUTCHours() - returns a date's hour in the UTC time zone.

getUTCMinutes() - returns a date's minutes in the UTC time zone.

getUTCSeconds() - returns a date's seconds in the UTC time zone.

Converting UTC to Local Time

We must use the getTimezoneOffset() method to convert UTC date time to local date time. This method returns the time difference in minutes between UTC and local time. This difference in minutes can then be used to convert the UTC date time to the local date time.

Example

For example, the following code will convert a UTC date time into the local date time −

<!DOCTYPE html>
<html>
<body>
   <div id="result"></div>
   <script>
      var utc = new Date();
      var offset = utc.getTimezoneOffset();
      var local = new Date(utc.getTime() + offset * 60000);
      document.getElementById("result").innerHTML = local;
   </script> 
</body>
</html>

We can see a New Date object called "utc" here, which holds the current UTC date and time. Then we used the getTimezoneOffset() function to calculate the time difference in minutes between UTC and local time. Finally, we calculated the local time by adding this amount to the UTC time in milliseconds.

Similarly, we can convert a specified UTC date time into a local date time. To accomplish the same thing, simply supply the UTC date and time as parameters to the Date() function Object() { [native code] }. Now, let's see the code to convert a UTC date time of "2018-11-12 12:00:00" to a local date time −

Example

<!DOCTYPE html>
<html>
<head>
   <title>Date Example</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var utc = new Date("2018-11-12 12:00:00");
      var offset = utc.getTimezoneOffset();
      var local = new Date(utc.getTime() + offset * 60000);
      document.getElementById("result").innerHTML = "UTC : " + utc + "<br>" + "Local : " + local;
   </script>
</body>
</html>

We have passed the UTC date and time as a string to the Date() constructor. We then use the same method as before to convert the UTC date time into the local date time.

Converting Local Time to UTC

Now, how can we get from local time to UTC? To convert local date time to UTC date time, we can use the getTimezoneOffset() method once more. Since this function returns the time difference in minutes between UTC and local time. This difference number can be used to convert the local date time to the UTC date time.

Example

For example, the following code will convert a local date time into the UTC date time −

<!DOCTYPE html>
<html>
<body>
   <div id="result"></div>
   <script>
      var local = new Date();
      var offset = local.getTimezoneOffset();
      var utc = new Date(local.getTime() - offset * 60000);
      document.getElementById("result").innerHTML = utc;
   </script>
</body>
</html>

Here, in the code above we first created a new Date object called "local" which contains the current local date and time. Then, we used the getTimezoneOffset() method to get the time difference between UTC and the local time in minutes. After subtracting this value from the local time in milliseconds we got the UTC time.

We can also convert a specific local date time into a UTC date time by passing the local date and time as arguments to the Date() constructor. For example, the following code will convert a local date time of "2018-11-12 12:00:00" into the UTC date time −

Example

<!DOCTYPE html>
<html>
<body>
   <div id="result"></div>
   <script>
      var local = new Date("2018-11-12 12:00:00");
      var offset = local.getTimezoneOffset();
      var utc = new Date(local.getTime() - offset * 60000);
      document.getElementById("result").innerHTML = utc;
   </script> 
</body>
</html>

We pass the local date and time as a string to the Date() constructor. We then use the same method as before to convert the local date time into the UTC date time.

Conclusion

In this tutorial, we learned how to convert UTC date time into local date time using JavaScript. We also learned that the JavaScript Date class provides several methods to work with dates and times, such as getTimezoneOffset() which can be used to convert UTC date time into local date time. We also learned how to convert local date time into UTC date time using the same method in this blog. It is important to note that timezone handling is an important aspect of any web application and it is important to convert the time properly so that it can be displayed correctly to the user.

Updated on: 21-Feb-2023

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements