The toString Method

When a Date object is created, a number of methods allow you to operate on it. Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of objects, using either local time or UTC (universal, or GMT) time. More about date methods later.


When you display a date object in HTML, it is automatically converted to a string, regardless if the toString() method is used or not:

HTML file: Displayed by browser:
<body>
<p>Without using the toString() method:</p>
<p id="demo"></p>
<hr />
<p>Using the toString() method:</p>
<p id="demo1"></p>

<script>
var d = new Date();
document.getElementById("demo").innerHTML = d;
document.getElementById("demo1").innerHTML = d.toString();
</script>
</body>

Without using the toString() method:


Using the toString() method:


BackTable of ContentsNext