Home >>JavaScript Date Object Methods >JavaScript Date toString() Method

JavaScript Date toString() Method

JavaScript Date toString() Method

JavaScript toString() methodin JavaScript is an inbuilt function which is used to convert a Date object in the form of String.

Syntax:
Date.toString()

Browser Support

Method Chrome Edge Firefox Safari Opera
toString() Yes Yes Yes Yes Yes
Here is an Example of JavaScript Date toString() Method:
<html>
<body>
<button onclick="mydtstring()">click me</button>
<p id="datestr"></p>
<script>
function mydtstring()
{
  var x = new Date();
  var z = x.toString();
  document.getElementById("datestr").innerHTML = z;
}
</script>
</body>
</html>
Output:

Example 2:
<html>
<body>
<script> 
var dateobj1 = new Date('1'); 
var dateobj2 = new Date('2, 2'); 
var dateobj3 = new Date('3, 10, 2'); 
var dateobj4 = new Date('4, 5, 2, 7'); 
var dateobj5 = new Date('4, 5, 3, 10:24:12'); 
var dateobj6 = new Date('5, 5, 4, 0:0'); 

var AB = dateobj1.toString(); 
var BC = dateobj2.toString(); 
var CD = dateobj3.toString(); 
var DE = dateobj4.toString(); 
var EF = dateobj5.toString(); 
var FG = dateobj6.toString();  

document.write(AB + "<br>"); 
document.write(BC + "<br>"); 
document.write(CD + "<br>"); 
document.write(DE + "<br>"); 
document.write(EF + "<br>"); 
document.write(FG); 
</script>					 
</body>
</html>
Output:

No Sidebar ads