Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Difference

Status
Not open for further replies.

shultz

Programmer
Oct 28, 2001
42
HK
How can I find the difference of two dates in Javascript. and also how can I find the final date when a certain number of hours are substracted from the current time.

For example:

If I want to find the difference in days between 1st of Jan 2002 and today, how can I do it.

Another One:

If I reduce 5 hours say from 3am today, how can I find the resulting date and time.

any idea?

thanks in advance.
 
<script>
var dteNow = new Date();
var dteJan = new Date(2002,0,1);
var diff = dteNow.getTime() - dteJan.getTime();
alert('Difference between now and 1 jan 2002 is:\n' + diff + ' milliseconds');
alert('Difference between now and 1 jan 2002 is:\n' + diff/1000 + ' seconds');
alert('Difference between now and 1 jan 2002 is:\n' + diff/6000 + ' minutes');
alert('Difference between now and 1 jan 2002 is:\n' + diff/3600000 + ' hours');
alert('Difference between now and 1 jan 2002 is:\n' + diff/86400000 + ' days');


</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top