
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- How do you convert a JavaScript date to UTC?
- How to get offsetdatetime local date in android using offset date time API class?
- How to convert date and time into int in Android sqlite?
- Pandas program to convert a string of date into time
- How to get current date and time using JavaScript?
- How to get local time and date in android using LocalDateTime API class?
- How to get the current local date and time in Kotlin?
- How to convert JS date time to MySQL datetime?
- How to Convert Epoch Time to Date in SQL?
- How to convert JSON results into a date using JavaScript?
- How to set local date/time in a table using LocalDateTime class in Java?
- Python program to convert local time into GMT
- How to set default date time as system date time in MySQL?
- How to get local date in android using local date API class?
- How to convert a date to a string using the universal time convention?
