CoderZone.org

Category: >> JavaScript >> Calculate time difference between two dates Bookmark and Share

<< lastnext >>

Snippet Name: Calculate time difference between two dates

Description: Calculates the time difference between two dates.

Also see:
» Calculate days between any two dat...
» Get Relative Dates and Times
» Convert UK Dates To mySQL Format D...
» Calculate the difference between t...
» %TYPE vs %ROWTYPE - What's the dif...
» Dates: Oddball Stuff
» Date/Time Calculations: Get second...
» Date Functions: Add and Subtract f...
» Add and Subtract dates
» Select or match a range of dates
» Count days between dates
» Find days between dates #1

Comment: (none)

Author: CoderZone
Language: JAVASCRIPT
Highlight Mode: JAVASCRIPT
Last Modified: February 27th, 2009

function timeDifference(laterdate,earlierdate) {
    var difference = laterdate.getTime() - earlierdate.getTime();
 
    var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24
 
    var hoursDifference = Math.floor(difference/1000/60/60);
    difference -= hoursDifference*1000*60*60
 
    var minutesDifference = Math.floor(difference/1000/60);
    difference -= minutesDifference*1000*60
 
    var secondsDifference = Math.floor(difference/1000);
 
 document.write('difference = ' + daysDifference + ' day/s ' + hoursDifference + ' hour/s ' + minutesDifference + ' minute/s ' + secondsDifference + ' second/s ');
 
var laterdate = new Date(2000,0,1);     // 1st January 2000
var earlierdate = new Date(1998,2,13);  // 13th March 1998
 
timeDifference(laterdate,earlierdate);
}
 
Add a comment 
MikaOct 15th, 2010 10:01am
Super- just what i needed, thank you!!!
© coderzone.org | users online: 11