From Date will not be accept earlier than 13 months in javascript

前端 未结 2 1738
有刺的猬
有刺的猬 2021-01-29 04:22

Here i have \"From Date\" and \"To Date\" with submit button. And i\'m using telerik radate control for this project. So, I couldn\'t add that telerik control.

Here Ever

相关标签:
2条回答
  • 2021-01-29 04:52

    You can set the month to a date in the past and compare with that:

    var today = new Date(), // you can use only one Date, this is only for clarity
        back = new Date();
    back.setMonth( today.getMonth() - 13);
    return fromDate > back;
    

    Fixing the day as suggested by @mplungjan:

    var months = 13;
    if (back.getMonth() == (today.getMonth() - months % 12 + 12 + 1) % 12)
       back.setDate(-1);
    

    Updated fiddle: Use a clone of endDate instead of today to init back, then compare with startDate:

    var back = new Date(endDate);
    …
    return startDate > back;
    
    0 讨论(0)
  • 2021-01-29 04:57
    var toDate= urToDate;
    var fromDate= urFromDate;
    fromDate.setMonth(toDate.getMonth()+13);
    
    if(fromDate>toDate)
    {
    //Do your work
    }
    else
    {
    //from date is older
    }
    
    0 讨论(0)
提交回复
热议问题