how to subtract years from a date using JavaScript

 

To subtract number of years  from date using  JavaScript 

To subtract any number of years from JavaScript date object. first create a JavaScript date object (you can pass  also desired date in date object) then only subtract you need to subtract number of years from date object

   currentdate = new Date();
   totalYearsToSubtract = 2
   currentdate.setDate(currentdate.getFullYear() - totalYearsToSubtract);
   
   console.log("Two yeat back date");
   
   console.log(currentdate);

 

 

Years difference between two dates

 var yearsDifferenc = Date.now() - new Date('2020-01-11'));
 var newDateObj = new Date(yearsDifferenc); 
 var totalNumberOfYears = Math.abs(newDateObj.getUTCFullYear() - 1970);

Tags:

Share:

Related posts