To add years to a date, we can use the JavaScript getFullYear() method. The getFullYear() method gets the current year of a date object. We can then add more years to it and create a new Date using the Date() method.
var date1 = new Date();
var currYear = date1.getFullYear();
var currMonth = date1.getMonth();
var currDay = date1.getDate();
var currHours = date1.getHours();
var currMinutes = date1.getMinutes();
var currSeconds = date1.getSeconds();
var newDate = new Date(currYear + 5, currMonth, currDay, currHours, currMinutes, currSeconds);
//This will create a new date that will be 5 years ahead of the current date
We can also add negative milliseconds to subtract years from a date. Below are some examples of adding years to a date object with the help of the getTime() method.
var date1 = new Date();
var currYear = date1.getFullYear();
var currMonth = date1.getMonth();
var currDay = date1.getDate();
var currHours = date1.getHours();
var currMinutes = date1.getMinutes();
var currSeconds = date1.getSeconds();
var result1 = date1;
var result2 = new Date(currYear + 5, currMonth, currDay, currHours, currMinutes, currSeconds);
var result3 = new Date(currYear + 100, currMonth, currDay, currHours, currMinutes, currSeconds);
var result4 = new Date(currYear - 5, currMonth, currDay, currHours, currMinutes, currSeconds);
var result5 = new Date(currYear - 100, currMonth, currDay, currHours, currMinutes, currSeconds);
Which would result in the following:
result2 = Thu Jan 21 2027 16:20:29 GMT-0500 (Eastern Standard Time);
result3 = Wed Jan 21 2122 16:20:29 GMT-0500 (Eastern Standard Time);
result4 = Sat Jan 21 2017 16:20:29 GMT-0500 (Eastern Standard Time);
result5 = Sat Jan 21 1922 16:20:29 GMT-0500 (Eastern Standard Time);
Add Years to a Date Using JavaScript with a Click
In this example, we will let the user add years to the current date using an input field. We will use the getFullYear() method like we did above.
Here is our HTML setup:
How many years do you want to add to the current date and time?
Below is the JavaScript code which will take the user input using the onclick event and run the function below. The function will add the years to the current date like we did above. We will then update the #results div using the textContent property.
function addYears() {
var date1 = new Date();
var currYear = date1.getFullYear();
var currMonth = date1.getMonth();
var currDay = date1.getDate();
var currHours = date1.getHours();
var currMinutes = date1.getMinutes();
var currSeconds = date1.getSeconds();
var userYears = document.getElementById("userVal").value;
var newDate = new Date(currYear + Number(userYears), currMonth, currDay, currHours, currMinutes, currSeconds);
document.getElementById("results").textContent = newDate;
}
The final code and output for this example is below:
Code Output:
How many years do you want to add to the current date and time?
Full Code:
How many years do you want to add to the current date and time?
<script>
function addYears() {
var date1 = new Date();
var currYear = date1.getFullYear();
var currMonth = date1.getMonth();
var currDay = date1.getDate();
var currHours = date1.getHours();
var currMinutes = date1.getMinutes();
var currSeconds = date1.getSeconds();
var userYears = document.getElementById("userVal").value;
var newDate = new Date(currYear + Number(userYears), currMonth, currDay, currHours, currMinutes, currSeconds);
document.getElementById("results").textContent = newDate;
}
</script>
Hopefully this article has been useful in helping you understand how to Add Years to a Date Using JavaScript.
Click here for more information.