Check if a date within in range

╄→гoц情女王★ 提交于 2019-12-01 06:12:17
Chase

Change:

var endDate = today;

to:

var endDate = new Date(today);

See the posts here for how objects are referenced and changed. There are some really good examples that help explain the issue, notably:

Instead, the situation is that the item passed in is passed by value. But the item that is passed by value is itself a reference.

JSFiddle example

function isLinkExpiryDateWithinRange( value ) {
    // format: mm.dd.yyyy;
    value = value.split(".");
    var todayDate = new Date(),
        endDate = new Date( todayDate.getFullYear(), todayDate.getMonth() + 6, todayDate.getDate() +1 );
        date = new Date(value[2], value[0]-1, value[1]);

    return todayDate < date && date < endDate;
}

isLinkExpiryDateWithinRange("12.24.2012"); // true
isLinkExpiryDateWithinRange("12.24.2020"); // false

Below function checks if date selected is within 5 days from today. Date format used is "DD-MM-YYYY", you can use any format by changing value.split('-')[1] order and split character.

function showMessage() {
        var value = document.getElementById("invoiceDueDate").value;
        var inputDate = new Date(value.split('-')[2], value.split('-')[1] - 1, value.split('-')[0]);
        var endDate = new Date();
        endDate.setDate(endDate.getDate() + 5);// adding 5 days from today
        if(inputDate < endDate) {
            alert("If the due date selected for the invoice is within 5 days, and express settlement fee will apply to this transaction.");
        }

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!