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
You need to use getFullYear()
and then generate new Date
var d = new Date(2012, 7, 25);
d.setFullYear(d.getFullYear() - 1);
Use this :
var date = new Date();
var intYear = date.getFullYear() - 1;
this solution will help.
new Date(new Date().setFullYear(new Date().getFullYear() - 1));
var today = new Date();
var curyear = today.getFullYear();
var curyearMonth = today.getMonth() + 1;
var curyearDay = today.getDate();
var lastYear = curyear - 1;
if ((curyearMonth == 2) && (curyearDay == 29)) {
curyearDay = 28;
}
var lastYearDisplay = ("0000" + lastYear.toString()).slice(-4) + "-" + ("00" + curyearMonth.toString()).slice(-2) + "-" + ("00" + curyearDay.toString()).slice(-2);
alert("LastWeekDate : " + lastYearDisplay);
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}`;
var date = CALC.today(); //(function in my dev-enviro - gives today's date)
var year = formatDate(date, 'yyyy'); //year is 2016
year = parseInt(year); //make sure '2016' becomes an integer
year = year -1; // var year is now: 2015.
//its been a while, but that's what I did from memory.