Skip to content

Instantly share code, notes, and snippets.

@zpao
Created December 31, 2011 00:12
Show Gist options
  • Save zpao/1542157 to your computer and use it in GitHub Desktop.
Save zpao/1542157 to your computer and use it in GitHub Desktop.
function datediff(aDate1, aDate2) {
var diff = Math.abs(aDate2 - aDate1);
var milliseconds = diff % 1000;
var seconds = Math.floor(diff / 1000 % 60);
var minutes = Math.floor(diff / (1000 * 60) % 60);
var hours = Math.floor(diff / (1000 * 60 * 60) % 24);
var days = Math.floor(diff / (1000 * 60 * 60 * 24) % 365);
return [
("00" + days).slice(-3), // DDD
("0" + hours).slice(-2), // HH
("0" + minutes).slice(-2), // MM
("0" + seconds).slice(-2), // SS
].join(":");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment