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 date objects, using either local time or UTC (universal, or GMT) time.
Displaying Dates
JavaScript will (by default) output dates in full text string format:
When you display a date object in HTML, it is automatically converted to a string, with the toString()
method.
Example
document.getElementById("demo").innerHTML = d;
Same as:
document.getElementById("demo").innerHTML = d.toString();
The toUTCString()
method converts a date to a UTC string (a date display standard).
Example
document.getElementById("demo").innerHTML = d.toUTCString();
The toDateString()
method converts a date to a more readable format:
Example
document.getElementById("demo").innerHTML = d.toDateString();
The toISOString()
method converts a date to a string, using the ISO standard format:
Example
document.getElementById("demo").innerHTML = d.toISOString();
Practice Excercise Practice now