Calculate difference between two dates
In JavaScript, you can easily get the difference between two dates. This is
because when you create a date object, it's automatically converted to a number,
specifically, the number of milliseconds passed since 1/1/1970 GMT to the date.
So here's an example that shows how many days have passed since January 1st,
1998:
<script>
var today=new Date()
var day1998=new Date(1998,0,1)
var difference=today-day1998//unit is milliseconds
formatdifference=Math.round(difference/1000/60/60/24) //now unit is days
document.write(formatdifference)
</script>
Easy enough right? One script I like that displays the difference between two
dates in a variety of units is The Ultimate Age calculator:
http://www.javascriptkit.com/script/script2/calculateage.shtml
|