Date difference in Javascript (ignoring time of day)

前端 未结 15 2401
无人共我
无人共我 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条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 16:05

    If you want whole days for your student camera rental example ...

    function daysBetween(first, second) {
    
        // Copy date parts of the timestamps, discarding the time parts.
        var one = new Date(first.getFullYear(), first.getMonth(), first.getDate());
        var two = new Date(second.getFullYear(), second.getMonth(), second.getDate());
    
        // Do the math.
        var millisecondsPerDay = 1000 * 60 * 60 * 24;
        var millisBetween = two.getTime() - one.getTime();
        var days = millisBetween / millisecondsPerDay;
    
        // Round down.
        return Math.floor(days);
    }
    

提交回复
热议问题