Find out the previous year date from current date in javascript

后端 未结 12 1364
执念已碎
执念已碎 2021-02-02 07:43

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

<
相关标签:
12条回答
  • 2021-02-02 08:23

    You need to use getFullYear()

    and then generate new Date

    var d = new Date(2012, 7, 25);
    d.setFullYear(d.getFullYear() - 1);
    
    0 讨论(0)
  • 2021-02-02 08:24

    Use this :

     var date = new Date();
     var intYear = date.getFullYear() - 1;
    
    0 讨论(0)
  • 2021-02-02 08:28

    this solution will help.

    new Date(new Date().setFullYear(new Date().getFullYear() - 1));
    
    0 讨论(0)
  • 2021-02-02 08:28
    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);
    
    0 讨论(0)
  • 2021-02-02 08:30

    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}`;
    
    0 讨论(0)
  • 2021-02-02 08:30
    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.
    
    0 讨论(0)
提交回复
热议问题