Today’s date is used by running the function today()
when the page loads:
function today() {
const now = new Date();
const firstPara = document.getElementById('today');
firstPara.innerHTML = `Today is <i>${now.toDateString()}</i>`;
}
today();
A current time and date stamp can be created using:
const nuDate = new Date();
function dateOne() {
document.getElementById('date1').innerHTML = new Date();
}
Today ?
Formatting
There are a number of methods that can be used to get one part of the Date()
object:
Using const now = new Date()
code | current value |
what it does |
---|---|---|
now.getFullYear |
the year in 4 digits | |
now.getMonth |
the month in single or double digits starting at 0 | |
now.getDate |
the day of the month from 1 to 31 | |
now.getDay |
the day of the week counting from 0 to 6 | |
now.getSeconds |
seconds past the current minute | |
now.getMinutes |
minutes past the hour | |
now.getHours |
hours from 0 to 23 count starting at midnight | |
now.getYear |
DEPRECATED |
What year is it?
Using the getFullYear()
method.
function dateTwo() {
let d = new Date();
document.getElementById('date2').innerHTML = d.getFullYear();
}