Find out the previous year date from current date in javascript

Viewed 78806

I need to find out the previous year date from current date and then set as minDate in jQuery UIdatepicker in javascript

My date formaqt is dd-mm-yy

Ie

  • current date is 25-07-2012
  • I need to get 25-07-2011
12 Answers

Try this

var d = new Date();
var pastYear = d.getFullYear() - 1;
d.setFullYear(pastYear);
console.log(d);

this solution will help.

new Date(new Date().setFullYear(new Date().getFullYear() - 1));

you can define a new date or an existing date as d variable

var d = new Date();

then you can put date, month and year to the new date string using ${variableName}, Also you must add 1 to d.getMonth() and substract 1 from d.getFullYear()

var previousYearDate = `${d.getDate()}-${d.getMonth() + 1}-${d.getFullYear() - 1}`;

I found this very useful:

new Date(new Date().setFullYear(new Date().getFullYear() - 1)).toISOString().slice(0, 10);
Related