Page Stats
Visitor: 341
JavaScript Date Functions
Example 1: Display Date and Time on Button Click.
var d = new Date();
document.getElementById("demo").innerHTML = d;
document.getElementById("demo").innerHTML = d.toDateString();
Date and Time can be extract in parts also. Common javascript date and time functions are:
Attributes | Description |
---|---|
getDate() | Return the day as a number (1-31) |
getMonth() | Return the month (0-11) |
getFullYear() | Return the four digit year (yyyy) |
getDay() | Return the weekday a number (0-6) |
getHours() | Return the hour (0-23) |
getMinutes() | Return the minutes (0-59) |
getSeconds() | Return the seconds (0-59) |
getMilliseconds() | Return the milliseconds (0-999) |
getTime() | Return the time (milliseconds since January 1, 1970) |
var d = new Date();
document.getElementById("demo").innerHTML = d.getDay();
document.getElementById("demo").innerHTML = d.getFullYear();
document.getElementById("demo").innerHTML = d.getTime();
The setDate() function can be used to add days to a date:
var d = new Date();
d.setDate(d.getDate() + 50);
document.getElementById("demo").innerHTML = d;