To literally get the number of days between those two date time values, you would do this:
Code:
(new Date(2006,11,10,9,10,21).getTime()-new Date(2006,11,6,8,20,21).getTime())/(1000*60*60*24)
Of course, that works out to:
4.034722222222222 days
So you could use the Math.round() method:
Code:
Math.round((new Date(2006,11,10,9,10,21).getTime()-new Date(2006,11,6,8,20,21).getTime())/(1000*60*60*24))
which will give you 4 in this case. Alternatively, you could omit the H,M,S to begin with:
Code:
(new Date(2006,11,10).getTime()-new Date(2006,11,6).getTime())/(1000*60*60*24)
which will also give you 4 in this case. However, if it was really closer to 3 or 5 days, you no longer can tell that.
For flat rate per day charges with no allowance for partial days, for example, use Math.ceil() method:
Code:
Math.ceil((new Date(2006,11,10,9,10,21).getTime()-new Date(2006,11,6,8,20,21).getTime())/(1000*60*60*24))
which works out to 5 in this same case.
Finally, if partial days do not count, even if they consist of 23 hours, 59 minutes and 59 seconds - use Math.floor().
Bookmarks