JavaScript toGMTString method

This page discusses - JavaScript toGMTString method

JavaScript toGMTString method

JavaScript toGMTString method

        

toGMTString() method of JavaScript is used to convert any date into the string according to GMT( Greenwich Mean Time ) and returns the converted string.

Syntax :

 date_object.toGMTString();

Where date_object is any date object reference which is to be converted into string format according to the GMT.

To understand the use of toGMTString() method in a better way let's go with a simple example. In this example we have converted current date to the string format according to GMT.

Description of code :

In this example code we have created a button which calls the function "convertToGMTString()" on the click event, which is defined in the <script></script> tags. It calls the toGMTString() method by the current date object and converted date is shown in an alert message. Here is the full example code as follows :

<html>
<head>
<title>toGMTString() method </title>
 <script language="JavaScript">
  function convertToGMTString()
  {
    alert(new Date().toGMTString());
  }
 </script>
</head>
<body>
<div style="background: #ff9900; width:'100%';"
                     align="center">
  <font color="#0000ff" size="12pt">
	<b>toGMTString() Example</b>
  </font>
 </div>
  <center>
     <p>
       Click on the following button to show date into
               GMT string
     </p>
       <input type="button" value="Convert current date 
    to GMT String" onClick="return convertToGMTString();"/>
  </center>
</body>
</html>

Output :

Click on the button "Convert current date to GMT String"

As you can see from the above output that it shows the converted current date in an alert message box.

Download Source Code