我需要从当前日期找出上一年的日期,然后在 javascript 中的 jQuery UIdatepicker 中设置为 minDate
我的日期格式是dd-mm-yy
IE
- 当前日期是
25-07-2012
- 我需要得到
25-07-2011
我需要从当前日期找出上一年的日期,然后在 javascript 中的 jQuery UIdatepicker 中设置为 minDate
我的日期格式是dd-mm-yy
IE
25-07-2012
25-07-2011
尝试这个
var d = new Date();
var pastYear = d.getFullYear() - 1;
d.setFullYear(pastYear);
console.log(d);
这个解决方案会有所帮助。
new Date(new Date().setFullYear(new Date().getFullYear() - 1));
用这个 :
var date = new Date();
var intYear = date.getFullYear() - 1;
Datepicker 允许您输入一个数字作为minDate
选项,并将其用作当前日期的偏移量。所以你可以写:
minDate: -365
指定 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);
我发现这非常有用:
new Date(new Date().setFullYear(new Date().getFullYear() - 1)).toISOString().slice(0, 10);
您可以将新日期或现有日期定义为 d 变量
var d = new Date();
然后您可以使用 将日期、月份和年份添加到新的日期字符串中${variableName}
,还必须将 1 添加到 d.getMonth() 并从 d.getFullYear() 中减去 1
var previousYearDate = `${d.getDate()}-${d.getMonth() + 1}-${d.getFullYear() - 1}`;
为了避免 Date 对象(如果这是 OP 希望的):
var currDate = '25-07-2012';
var dateParts = currDate.split('-');
dateParts[2] = parseInt(dateParts[2], 10) - 1;
alert(dateParts.join('-'));
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.
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;
}