简体   繁体   English

在javascript中从当前日期找出上一年的日期

[英]Find out the previous year date from current date in javascript

I need to find out the previous year date from current date and then set as minDate in jQuery UIdatepicker in javascript我需要从当前日期找出上一年的日期,然后在 javascript 的 jQuery UIDatepicker 中设置为 minDate

My date formaqt is dd-mm-yy我的日期格式是dd-mm-yy

Ie IE

  • current date is 25-07-2012当前日期是25-07-2012
  • I need to get 25-07-2011我需要得到25-07-2011

You need to use getFullYear()您需要使用getFullYear()

and then generate new Date然后生成新的日期

var d = new Date(2012, 7, 25);
d.setFullYear(d.getFullYear() - 1);

Try this试试这个

var d = new Date();
var pastYear = d.getFullYear() - 1;
d.setFullYear(pastYear);
console.log(d);

this solution will help.这个解决方案会有所帮助。

new Date(new Date().setFullYear(new Date().getFullYear() - 1));

Use this:用这个:

 var date = new Date();
 var intYear = date.getFullYear() - 1;

Datepicker allows you to put a number as the minDate option, and it uses that as an offset from the current date. Datepicker 允许您将数字作为minDate选项,并将其用作当前日期的偏移量。 So you can write:所以你可以写:

minDate: -365

to specify 1 year ago.指定 1 年前。 This doesn't take leap years into account, though.不过,这并没有考虑闰年。

I need to find out the previous year date from current date and then set as minDate in jQuery UIdatepicker in javascript我需要从当前日期找出上一年的日期,然后在 javascript 中的 jQuery UIdatepicker 中设置为 minDate

My date formaqt is dd-mm-yy我的日期格式是dd-mm-yy

Ie IE

  • current date is 25-07-2012当前日期是25-07-2012
  • I need to get 25-07-2011我需要得到25-07-2011

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.如果您使用Date对象,则可以使用set[Full]Year方法轻松减去年份。

you can define a new date or an existing date as d variable您可以将新日期或现有日期定义为 d 变量

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

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);

I found this very useful:我发现这非常有用:

new Date(new Date().setFullYear(new Date().getFullYear() - 1)).toISOString().slice(0, 10);

To avoid the Date object (if that is what OP wishes):为了避免 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM