Reputation: 487
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-2012
25-07-2011
Upvotes: 44
Views: 99981
Reputation: 1
I found this very useful:
new Date(new Date().setFullYear(new Date().getFullYear() - 1)).toISOString().slice(0, 10);
Upvotes: 0
Reputation: 3520
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}`;
Upvotes: 1
Reputation: 2424
this solution will help.
new Date(new Date().setFullYear(new Date().getFullYear() - 1));
Upvotes: 13
Reputation: 795
Try this
var d = new Date();
var pastYear = d.getFullYear() - 1;
d.setFullYear(pastYear);
console.log(d);
Upvotes: 21
Reputation: 1
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;
}
Upvotes: -1
Reputation: 17080
You need to use getFullYear()
and then generate new Date
var d = new Date(2012, 7, 25);
d.setFullYear(d.getFullYear() - 1);
Upvotes: 67
Reputation: 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.
Upvotes: -1
Reputation: 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);
Upvotes: 0
Reputation: 780655
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.
Upvotes: 3
Reputation:
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('-'));
Upvotes: -1
Reputation: 664164
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.
Upvotes: 2
Reputation: 1171
Use this :
var date = new Date();
var intYear = date.getFullYear() - 1;
Upvotes: 5