Comparing date part only without comparing time in JavaScript

后端 未结 22 1417
春和景丽
春和景丽 2020-11-22 10:59

What is wrong with the code below?

Maybe it would be simpler to just compare date and not time. I am not sure how to do this either, and I searched, but I couldn\'t

相关标签:
22条回答
  • 2020-11-22 11:41

    As I don't see here similar approach, and I'm not enjoying setting h/m/s/ms to 0, as it can cause problems with accurate transition to local time zone with changed date object (I presume so), let me introduce here this, written few moments ago, lil function:

    +: Easy to use, makes a basic comparison operations done (comparing day, month and year without time.)
    -: It seems that this is a complete opposite of "out of the box" thinking.

    function datecompare(date1, sign, date2) {
        var day1 = date1.getDate();
        var mon1 = date1.getMonth();
        var year1 = date1.getFullYear();
        var day2 = date2.getDate();
        var mon2 = date2.getMonth();
        var year2 = date2.getFullYear();
        if (sign === '===') {
            if (day1 === day2 && mon1 === mon2 && year1 === year2) return true;
            else return false;
        }
        else if (sign === '>') {
            if (year1 > year2) return true;
            else if (year1 === year2 && mon1 > mon2) return true;
            else if (year1 === year2 && mon1 === mon2 && day1 > day2) return true;
            else return false;
        }    
    }
    

    Usage:

    datecompare(date1, '===', date2) for equality check,
    datecompare(date1, '>', date2) for greater check,
    !datecompare(date1, '>', date2) for less or equal check

    Also, obviously, you can switch date1 and date2 in places to achieve any other simple comparison.

    0 讨论(0)
  • You can use some arithmetic with the total of ms.

    var date = new Date(date1);
    date.setHours(0, 0, 0, 0);
    
    var diff = date2.getTime() - date.getTime();
    return diff >= 0 && diff < 86400000;
    

    I like this because no updates to the original dates are made and perfom faster than string split and compare.

    Hope this help!

    0 讨论(0)
  • 2020-11-22 11:42
    var fromdate = new Date(MM/DD/YYYY);
    var todate = new Date(MM/DD/YYYY);
    if (fromdate > todate){
        console.log('False');
    }else{
        console.log('True');
    }
    

    if your date formate is different then use moment.js library to convert the format of your date and then use above code for compare two date

    Example :

    If your Date is in "DD/MM/YYYY" and wants to convert it into "MM/DD/YYYY" then see the below code example

    var newfromdate = new Date(moment(fromdate, "DD/MM/YYYY").format("MM/DD/YYYY"));
    console.log(newfromdate);
    var newtodate = new Date(moment(todate, "DD/MM/YYYY").format("MM/DD/YYYY"));
    console.log(newtodate);
    
    
    0 讨论(0)
  • 2020-11-22 11:44

    This is the way I do it:

    var myDate  = new Date($('input[name=frequency_start]').val()).setHours(0,0,0,0);
    var today   = new Date().setHours(0,0,0,0);
    if(today>myDate){
        jAlert('Please Enter a date in the future','Date Start Error', function(){
            $('input[name=frequency_start]').focus().select();
        });
    }
    
    0 讨论(0)
  • 2020-11-22 11:46

    This will help. I managed to get it like this.

    var currentDate = new Date(new Date().getFullYear(), new Date().getMonth() , new Date().getDate())
    
    0 讨论(0)
  • 2020-11-22 11:48

    If you are truly comparing date only with no time component, another solution that may feel wrong but works and avoids all Date() time and timezone headaches is to compare the ISO string date directly using string comparison:

    > "2019-04-22" <= "2019-04-23"
    true
    > "2019-04-22" <= "2019-04-22"
    true
    > "2019-04-22" <= "2019-04-21"
    false
    > "2019-04-22" === "2019-04-22"
    true
    

    You can get the current date (UTC date, not neccesarily the user's local date) using:

    > new Date().toISOString().split("T")[0]
    "2019-04-22"
    

    My argument in favor of it is programmer simplicity -- you're much less likely to botch this than trying to handle datetimes and offsets correctly, probably at the cost of speed (I haven't compared performance)

    0 讨论(0)
提交回复
热议问题