• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Add Years to a Date Using JavaScript

Add Years to a Date Using JavaScript

January 21, 2022 Leave a Comment

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:

result1 = Fri Jan 21 2022 16:20:29 GMT-0500 (Eastern Standard Time);
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:

<p>How many years do you want to add to the current date and time?</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" onclick="addYears()" value="Submit">
<div id="results"></div>

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:

<p>How many years do you want to add to the current date and time?</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" onclick="addYears()" value="Submit">
<div id="results"></div>

<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.

Other Articles You'll Also Like:

  • 1.  Remove All Instances of Value From an Array in JavaScript
  • 2.  Adding CSS Important Property Using JavaScript
  • 3.  Using JavaScript to Add Items to Set
  • 4.  Using JavaScript to Show a Div
  • 5.  Using JavaScript to Get Radio Button Value
  • 6.  Using JavaScript to Count Even Numbers in an Array
  • 7.  Using JavaScript to Sort an Array of Strings
  • 8.  Using JavaScript to get Textarea Value
  • 9.  Using JavaScript to Remove Trailing Zeros From a Number or String
  • 10.  Using JavaScript to Check If String is a Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x