36

我需要从当前日期找出上一年的日期,然后在 javascript 中的 jQuery UIdatepicker 中设置为 minDate

我的日期格式是dd-mm-yy

IE

  • 当前日期是25-07-2012
  • 我需要得到25-07-2011
4

12 回答 12

54

你需要使用getFullYear()

然后生成新的日期

var d = new Date(2012, 7, 25);
d.setFullYear(d.getFullYear() - 1);
于 2012-07-25T12:28:44.587 回答
17

尝试这个

var d = new Date();
var pastYear = d.getFullYear() - 1;
d.setFullYear(pastYear);
console.log(d);
于 2016-09-27T06:52:02.663 回答
11

这个解决方案会有所帮助。

new Date(new Date().setFullYear(new Date().getFullYear() - 1));
于 2019-08-13T13:37:45.520 回答
5

用这个 :

 var date = new Date();
 var intYear = date.getFullYear() - 1;
于 2012-07-25T12:30:16.393 回答
3

Datepicker 允许您输入一个数字作为minDate选项,并将其用作当前日期的偏移量。所以你可以写:

minDate: -365

指定 1 年前。不过,这并没有考虑闰年。

于 2012-10-27T07:02:14.250 回答
2

对于字符串:

curdate.substr(0, 6)+(curdate.substr(6)-1);

如果您使用Dateobject,您可以使用该方法轻松减去一年set[Full]Year

于 2012-07-25T12:28:01.667 回答
0
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);
于 2014-10-29T07:35:04.453 回答
0

我发现这非常有用:

new Date(new Date().setFullYear(new Date().getFullYear() - 1)).toISOString().slice(0, 10);
于 2022-02-11T07:47:41.667 回答
0

您可以将新日期或现有日期定义为 d 变量

var d = new Date();

然后您可以使用 将日期、月份和年份添加到新的日期字符串中${variableName},还必须将 1 添加到 d.getMonth() 并从 d.getFullYear() 中减去 1

var previousYearDate = `${d.getDate()}-${d.getMonth() + 1}-${d.getFullYear() - 1}`;
于 2020-08-31T04:35:49.003 回答
-1

为了避免 Date 对象(如果这是 OP 希望的):

var currDate = '25-07-2012';
var dateParts = currDate.split('-');
dateParts[2] = parseInt(dateParts[2], 10) - 1;
alert(dateParts.join('-'));​
于 2012-07-25T12:50:07.500 回答
-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.
于 2016-02-19T00:22:33.387 回答
-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;
    }
于 2016-07-18T15:49:44.677 回答