Date.toString() function in JavaScript


The Date object is a data type built into the JavaScript language. Date objects are created with the new Date( ) as shown below.

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

The toString() function of the date object returns the string representation of the date object.

Syntax

Its Syntax is as follows

dateObj.toString()

Try the following example.

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('September 26, 89 12:4:25:96');
      document.write("Current Date: "+dateObj.toString());
   </script>
</body>
</html>

Output

Current Date: Tue Sep 26 1989 12:04:25 GMT+0530 (India Standard Time)

Try the following example.

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date();
      document.write("Current Date: "+dateObj.toString());
   </script>
</body>
</html>

Output

Current Date: Thu Oct 18 2018 15:20:42 GMT+0530 (India Standard Time)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 25-Jun-2020

27 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements