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
I had the same problem with Angular 7. Following is my solution in typescript.
new Date(new Date().setFullYear(new Date().getFullYear() - 1))
For strings:
curdate.substr(0, 6)+(curdate.substr(6)-1);
If you'd use a Date object, you could easily subtract a year with the set[Full]Year
method.
Try this
var d = new Date();
var pastYear = d.getFullYear() - 1;
d.setFullYear(pastYear);
console.log(d);
To avoid the Date object (if that is what OP wishes):
var currDate = '25-07-2012';
var dateParts = currDate.split('-');
dateParts[2] = parseInt(dateParts[2], 10) - 1;
alert(dateParts.join('-'));
function getTodayDate() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is not 0!
var yyyy = today.getFullYear();
if (dd < 10) { dd = '0' + dd }
if (mm < 10) { mm = '0' + mm }
today = yyyy + '-' + mm + '-' + dd;
return today;
};
function getYearAgo(){
var lastYear = new Date();
var dd = lastYear.getDate();
var mm = lastYear.getMonth() + 1; //January is not 0!
var yyyy = lastYear.getFullYear(getTodayDate) - 1;
if (dd < 10) { dd = '0' + dd }
if (mm < 10) { mm = '0' + mm }
lastYear = yyyy + '-' + mm + '-' + dd;
return lastYear;
}
Datepicker allows you to put a number as the minDate
option, and it uses that as an offset from the current date. So you can write:
minDate: -365
to specify 1 year ago. This doesn't take leap years into account, though.