Date difference in Javascript (ignoring time of day)

前端 未结 15 2358
无人共我
无人共我 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:22

    You could adjust the start date to midnight of the same day, then calculate the number of 24-hour periods between the dates. Then, you would take the ceiling or floor of that number depending on whether you want to count any part of a day as a whole day or not.

    function dateDiff(startDate, endDate) {
        // set startDate to the beginning of the day
        startDate = new Date(
            startDate.getFullYear(),
            startDate.getMonth(),
            startDate.getDate());
    
        return Math.floor((endDate - startDate) / 1000*60*60*24);
    }
    

提交回复
热议问题