javascript check end date is greater than or equal

2020-03-03 05:03发布

Is it possible to check whether an end date is greater than or equal to a start date in Javascript? My dates are strings in the format 'dd/mm/yyyy'.

标签: javascript
10条回答
乱世女痞
2楼-- · 2020-03-03 05:51
function isDate(value)
            {
                var fromDate = document.getElementById("fromDate").value
                var toDate= document.getElementById("toDate").value
                //var curr_Date= new SimpleDateFormat("dd/mm/yyyy");


            var dateRegEx = null;
            dateRegEx = new RegExp(/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g);

            if (dateRegEx.test(fromDate)){
            }
            else{
                alert("Invalid from date");
                return false;
            }
            dateRegEx = new RegExp(/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g);
            if(dateRegEx.test(toDate)) {
            }
            else{
                alert("Invalid to date");
                return false;
            }
            var stDate = new Date(fromDate);
            var enDate = new Date(toDate);
            var compDate = enDate - stDate;
            //var fdate=enDate-curr_Date;

            if(compDate >= 0)
                return true;
            else 
            {
                alert("To Date cannot be smaller than From Date");
                return false;
            }




            /**/
        }

This will work for Leap years also..in dd/mm/yyyy format(not any other format).

查看更多
地球回转人心会变
3楼-- · 2020-03-03 05:51

First use this function will convert string to Date type in js:

function common_getDateFromUI(str) {
    var arr = str.split("/");
    var returnDate = new Date(arr[2], arr[1] - 1, arr[0], 0, 0, 0, 0);
    return returnDate;
}

Second: after you get the javascript date type, you just compare it as normal type like date1 > date2 or date1 == date2.
Or use this function to get the difference date between date:

function CalendarDays(startDate, endDate) {
    if (endDate < startDate)
        return 0;

    // Calculate days between dates
    var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
    startDate.setHours(0, 0, 0, 1); // Start just after midnight
    endDate.setHours(23, 59, 59, 999); // End just before midnight
    var diff = endDate - startDate; // Milliseconds between datetime objects
    var days = Math.round(diff / millisecondsPerDay);

    return days;
}

Follow this link is a simple demo to get difference days between dates. Link demo here

查看更多
smile是对你的礼貌
4楼-- · 2020-03-03 05:52

Took me some time to find, but JQuery implements this exact functionality with DatePicker date-range. (Source code available in link as well.)

Moment.js also handles date comparisons very well using the diff function.

查看更多
做自己的国王
5楼-- · 2020-03-03 06:00

Most simple way to do this.

function endAfterStart(start, end) {
    var startDate = new Date(start);
    var endDate   = new Date(end);

    return endDate.getTime() >= startDate.getTime();
} 
查看更多
登录 后发表回答