Open In App

JavaScript Date toGMTString() Method

Last Updated : 19 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The date.toGMTString() method is used to convert the given date object’s contents into a string according to the Greenwich Mean Time GMT. The date object is created using the date() constructor.

Note: This method has been deprecated and has been replaced by the toUTCString() method.

Syntax:

dateObj.toGMTString()

Parameters: This method does not accept any parameter. It is just used along with a Date object created using the Date() constructor whose contents are converted into a string. 

Return Values: It returns the converted string according to the Greenwich Mean Time GMT.

The below examples show the use of the Date toGMTString() method.

Example 1: In this example, we will convert a given time and date to GMT.

Javascript




<script>
    // Here a date has been assigned
    // while creating Date object
    var dateobj = new Date('October 15, 1996 05:35:32');
     
    // Contents of above date object is converted
    // into a string using toGMTString() method.
    var B = dateobj.toGMTString();
     
    // Printing the converted string.
    console.log(B);
</script>


Output:

Tue, 15 Oct 1996 00:05:32 GMT

Example 2: Here nothing is passed as a parameter while creating the date object but the toGMTString() method returns the current day, month, date, year, and time.

Javascript




<script type="text/javascript">
    var dt = new Date();
    console.log( "Formatted Date : " + dt.toGMTString() );
</script>


Output:

Formatted Date : Mon, 09 Jan 2023 05:54:54 GMT

We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads