getTimezoneOffset(). Learn more about it with the explanation and examples below.
What is UTC?
UTC stands for the English word “Universal Time Coordinated” and the French word “Temps Universel Coordonné”. Time UTC stands for Universal Time Coordinated, chosen by International Metrology (BIPM) as the infrastructure for locating time.
UTC was born based on the old standard time, GMT – Universal time.
The world is divided into however short 24 time periods. We sometimes designate their times as ½, according to the locator address.
Create current time using the Date() method
We can use the
new Date() constructor with one of the following parameters.
var now = new Date(); console.log(now)
Tue Sep 20 2022 00:04:29 GMT+0700 (Indochina Time)
The above example uses the
Date() method to get the current time value and assign it to the
now variable. We then print that value to the console.
Using toUTCString() method
toUTCString() method converts a date into a string using the Universal Time Convention. You can use
toUTCString() method to convert a date to a UTC string.
var now = new Date(); var utc = now.toUTCString(); console.log(utc);
Mon, 19 Sep 2022 17:19:21 GMT
So after you use the
toUTCString() method with the current time, it converts a date to a string, using the universal time zone.
Using getTimezoneOffset() method
In this example, we use the
getTimezoneOffset() method to convert a date to UTC. This method will return the time difference between the current system time zone and UTC in minutes.
var now = new Date(); var utc = new Date(now.getTime() + now.getTimezoneOffset() * 60000); console.log(utc);
Mon Sep 19 2022 17:19:21 GMT+0700 (Indochina Time)
utc variable is calculated by using the current time plus the user’s current time zone to get the current UTC.
Maybe you are interested:
Name of the university: PTIT