Create a new Date() object and call getFullYear():
new Date().getFullYear() // returns the current year
Example usage: a page footer that always shows the current year:
document.getElementById("year").innerHTML = new Date().getFullYear();
footer {
text-align: center;
font-family: sans-serif;
}
<footer>
<span id="year"></span> by Stack Overflow
</footer>
See also, the Date() constructor's full list of methods.
Create a new Date() object and call getFullYear():
new Date().getFullYear() // returns the current year
Example usage: a page footer that always shows the current year:
document.getElementById("year").innerHTML = new Date().getFullYear();
footer {
text-align: center;
font-family: sans-serif;
}
<footer>
<span id="year"></span> by Stack Overflow
</footer>
See also, the Date() constructor's full list of methods.
// Return today's date and time
var currentTime = new Date()
// returns the month (from 0 to 11)
var month = currentTime.getMonth() + 1
// returns the day of the month (from 1 to 31)
var day = currentTime.getDate()
// returns the year (four digits)
var year = currentTime.getFullYear()
// write output MM/dd/yyyy
document.write(month + "/" + day + "/" + year)
This will create a Date exactly one year in the future with just one line. First we get the fullYear from a new Date, increment it, set that as the year of a new Date. You might think we'd be done there, but if we stopped it would return a timestamp, not a Date object so we wrap the whole thing in a Date constructor.
new Date(new Date().setFullYear(new Date().getFullYear() + 1))
You should use getFullYear() instead of getYear(). getYear() returns the actual year minus 1900 (and so is fairly useless).
Thus a date marking exactly one year from the present moment would be:
var oneYearFromNow = new Date();
oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);
Note that the date will be adjusted if you do that on February 29.
Similarly, you can get a date that's a month from now via getMonth() and setMonth(). You don't have to worry about "rolling over" from the current year into the next year if you do it in December; the date will be adjusted automatically. Same goes for day-of-month via getDate() and setDate().