Getting a Time Delta

var atime = new Date("2014-01-05T08:03:22.356Z"); var btime = new Date("2014-01-05T09:08:57.758Z"); var delta = Math.abs(btime - atime); //3935402ms var totalSec = Math.floor(delta * .001); //3935s var hours = Math.floor(totalSec / 3600); //1hr var minutes = Math.floor(totalSec / 60) % 60; //5min var seconds = totalSec % 3600 % 60; //35sec

Date objects essentially represent the number of milliseconds from Jan 1, 1970. Therefore, you can get the time difference between two Date objects by subtracting them from each other. The result is a number that represents the millisecond difference between the two dates.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.