The easiest arithmetic is subtracting or adding two numbers (you may disagree), so finding the difference between two JavaScript date values is simple. You just find the difference and it returns the difference as a number. The result will be a date value in milliseconds, so you'll have to perform division to get the necessary value type (days, months, minutes, hours, etc.).
The following JavaScript calculates the number of days until a specific date. It subtracts the two date values (via getTime) and divides the result by the number of seconds in a day (86400000) to present the results in days:
<script type="text/javascript">
var d1 = new Date();
var d2 = new Date(2006, 6, 7);
var day = 1000*60*60*24;
var diff = Math.ceil((d2.getTime()-d1.getTime())/(day));
document.write("Days until vacation: " + diff);
</script>
Date arithmeticThe various properties of a Date value may be increased or decreased by adding or subtracting the necessary values via the appropriate property. For example, if you want to increase the value by one month, you would add one to the month property. The example in Listing D displays difference values for yesterday and tomorrow for the previous script.
Listing D
<script type="text/javascript">
var d1 = new Date();
var d2 = new Date(2006, 6, 7);
var day = 1000*60*60*24;
var diff = Math.ceil((d2.getTime()-d1.getTime())/(day));
document.write("Days until vacation: " + diff);
document.write("<br />");
d1.setDate(d1.getDate() + 1);
diff = Math.ceil((d2.getTime()-d1.getTime())/(day));
document.write("Tomorrow it will be " + diff + " days until
vacation.");
document.write("<br />");
d1.setDate(d1.getDate() - 2);
diff = Math.ceil((d2.getTime()-d1.getTime())/(day));
document.write("Yesterday, it was " + diff + " days until
vacation.");
</script>
The following output is displayed:Days until vacation: 50 Tomorrow it will be 49 days until vacation. Yesterday, it was 51 days until vacation.The time has come
Working with date and time values has its quirks that vary by platform, and Web development is no different. The JavaScript Data object provides an easy way to work with date and time values, but there are things to remember like the numbering of weekdays and months and formatting of some methods. They are not hard to remember once you are accustomed to its approach. A good thing to remember is the accuracy of date or time depends on the accuracy of the clock on the computer from which the page is being viewed.
Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.
This article was orginally published on TechRepublic.
Do you need help with Web Technologies? 



1
Sudip Dey - 05/03/08
codes are simple to understand....also give code which autorefresh the time....
I need your comments also....
» Report offensive content
2
Linda - 01/10/08
As part of my job, I add an expiration date to secured spec. The date is to be 1 week from the day someone prints or opens a specific spec. My problem is that it only works on 98 computers and not XP. I inherited the code and am not too familar with JavaScrip. Can you tell me why this does not work on XP computers?
Thanks for you time.
» Report offensive content