Comparing date part only without comparing time in JavaScript

后端 未结 22 1688
春和景丽
春和景丽 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: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);
    
    

提交回复
热议问题