The Wayback Machine - https://web.archive.org/web/20211103075129/https://www.geeksforgeeks.org/how-to-convert-a-javascript-date-to-utc/
Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to convert a JavaScript date to UTC?

  • Last Updated : 22 Apr, 2019

The Javascript date can be converted to UTC by using functions present in 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 which represents the Date based on the GMT (UT) time zone.

By default, JavaScript uses the browser’s time zone and display 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 javascript Date() constructor.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!


Syntax:

dateObj.toUTCString()

Example-1:






<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, 
                   initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" 
          content="ie=edge">
    <title>
      Document
  </title>
</head>
  
<body>
    <div id="date">
  
    </div>
</body>
<script>
    var d = new Date();
    var n = d.toUTCString();
    var e = document.querySelector("#date");
    e.innerHTML = n;
</script>
  
</html>

Output:

Fri, 12 Apr 2019 05:14:31 GMT

Example-2:




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    
    <meta http-equiv="X-UA-Compatible" 
          content="ie=edge">
    <title>
      Document
  </title>
</head>
  
<body>
    <div id="date">
  
    </div>
</body>
<script>
    var d = new Date();
    var e = document.querySelector("#date");
    e.innerHTML = d.toGMTString();
</script>
  
</html>

Output:

Fri, 12 Apr 2019 05:39:32 GMT

References
Javascript Date Object




My Personal Notes arrow_drop_up
Recommended Articles
Page :