Date difference in Javascript (ignoring time of day)

前端 未结 15 2399
无人共我
无人共我 2020-11-27 15:35

I\'m writing an equipment rental application where clients are charged a fee for renting equipment based on the duration (in days) of the rental. So, basically, (daily fee *

15条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 16:17

    use setHours() method, assuming number of days can never be zero :)

    function dateDiff1(startDate, endDate) {
        endDate.setHours(0);
        startDate.setHours(0);
        //assuming days cannt be 0.
    
        var x = ((endDate.getTime() - startDate.getTime()) / 1000*60*60*24);
        if (x <1 && x>=0) {
            return 1;
        }
        return x;
    }
    

提交回复
热议问题