Compare only the time portion of two dates, ignoring the date part

前端 未结 8 882
谎友^
谎友^ 2021-01-07 16:34

What\'s a good method to, given two Date objects, compare the difference between their time portion only, completely ignoring Year, Month and Day?

It\'s quite the op

8条回答
  •  感动是毒
    2021-01-07 17:01

    Dont know how good practiced or efficent is this one but i've written a simple function using the Date object that serves my purposes. It returns a true or false is the first date value bigger than the second. The inputs d1 && d2 are Date objects.

    function CompareTimes(d1, d2) {
                 if (d1.getHours() < d2.getHours()) {
                     return false;
                 }
                 if (d1.getHours() > d2.getHours()) {
                     return true;
    
                 } else {
                     return (d1.getMinutes() > d2.getMinutes());
                 }
    };
    

提交回复
热议问题