Created
September 3, 2015 11:06
-
-
Save Megabytemb/f3e800da47561bb27279 to your computer and use it in GitHub Desktop.
Human Date Difference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Date.daysBetween = function (date_now, date_future) { | |
// get total seconds between the times | |
var delta = Math.abs(date_future - date_now) / 1000; | |
delta_orig = delta; | |
// calculate (and subtract) whole days | |
var days = Math.floor(delta / 86400); | |
delta -= days * 86400; | |
// calculate (and subtract) whole hours | |
var hours = Math.floor(delta / 3600) % 24; | |
delta -= hours * 3600; | |
// calculate (and subtract) whole minutes | |
var minutes = Math.floor(delta / 60) % 60; | |
delta -= minutes * 60; | |
// what's left is seconds | |
var seconds = Math.floor(delta % 60); // in theory the modulus is not required | |
if (delta_orig < 60) { | |
return seconds == 1 ? seconds + " Second" : seconds + " Seconds"; | |
} | |
if (delta_orig < 60 * 60) { | |
return minutes == 1 ? minutes + " minute" : minutes + " Minutes"; | |
} | |
if (delta_orig < 24 * 60 * 60) { | |
return hours == 1 ? hours + " Hour" : hours + " Hours"; | |
} | |
return days == 1 ? days + " Day" : days + " Days"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment