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
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
25-07-201225-07-2011You need to use getFullYear()
and then generate new Date
var d = new Date(2012, 7, 25);
d.setFullYear(d.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.
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 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);
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;
}