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

前端 未结 2 1743
有刺的猬
有刺的猬 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;
    

提交回复
热议问题