How to Subtract One Year From a Date in JavaScript

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.

JavaScript is an object-oriented scripting language that is commonly used in web development due to its ability to dynamically render webpages. The use of objects in JavaScript can lead to simplification of many commands by grouping multiple related pieces of information together. One example of this simplification is subtracting one year from the current date, something that can be handled quite easily in JavaScript using objects.

Advertisement

Step 1

Designate a variable called "date" to store the current date:

Video of the Day

var date = new Date('25 Dec 2010');

Step 2

Output the result to the console to ensure the above step is correct:

Advertisement

console.log(date);

Step 3

In the "month" date type, within the "date" object, subtract 12 from the current number. This will give you a date 12 months in the past, stored as the current date:

Advertisement

Advertisement

date.setMonth(date.getMonth() – 12);

Step 4

Again, output the result. The new date should be exactly 12 months prior to the current date (in the example used above, the new date would be "25 Dec 2009"):

console.log(date);

Video of the Day

Advertisement

Advertisement

references

Report an Issue

screenshot of the current page

Screenshot loading...