baltazhor200401
11-07-2006, 12:48 AM
I want to asking about how to get a date difference in javascript :
example :
Let say we have two textbox, the first one we call start date and the second one we call last date.
start date contain data 2006-11-06 08:20:21 and last date contain 2006-11-10 09:10:21.
How can we get the value of 4 days between those data in the textbox...
Thank You very much for your answer...;)
jscheuer1
11-07-2006, 05:54 AM
To literally get the number of days between those two date time values, you would do this:
(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:
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:
(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:
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().
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.