Open In App

How to convert a JavaScript date to UTC?

Javascript date can be converted to UTC by using functions present in the Javascript Date object. The toUTCString() method is used to convert a Date object into a string, according to universal time. The toGMTString() returns a string that represents the Date based on the GMT (UT) time zone.

By default, JavaScript uses the browser’s time zone and displays a date as a full-text string: Fri Apr 12 2019 10:32:15 GMT+0530 (India Standard Time).



To create a new Date object we use the javascript Date() constructor. Javascript Date Object

Syntax:

dateObj.toUTCString()

Example 1: In this example, we are using the toUTCString() method.




let d = new Date();
let n = d.toUTCString();
console.log(n);

Output

Wed, 13 Dec 2023 06:40:12 GMT

Example 2: In this example, we are using the toGMTString() method.




let d = new Date();
console.log(d.toGMTString());

Output
Wed, 13 Dec 2023 06:40:00 GMT
Article Tags :