O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required