Created
May 5, 2009 16:18
-
-
Save sstephenson/107031 to your computer and use it in GitHub Desktop.
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
var DateHelper = { | |
timeAgoInWords: function(from) { | |
return this.distanceOfTimeInWords(new Date(), from); | |
}, | |
distanceOfTimeInWords: function(to, from) { | |
var distance_in_seconds = ((to - from) / 1000); | |
var distance_in_minutes = (distance_in_seconds / 60).floor(); | |
if (distance_in_minutes <= 0) return "less than a minute"; | |
if (distance_in_minutes == 1) return "a minute"; | |
if (distance_in_minutes < 45) return distance_in_minutes + " minutes"; | |
if (distance_in_minutes < 90) return "about 1 hour"; | |
if (distance_in_minutes < 1440) return "about " + Math.round(distance_in_minutes / 60) + " hours"; | |
if (distance_in_minutes < 2880) return "1 day"; | |
if (distance_in_minutes < 43200) return Math.round(distance_in_minutes / 1440) + " days"; | |
if (distance_in_minutes < 86400) return "about 1 month"; | |
if (distance_in_minutes < 525960) return Math.round(distance_in_minutes / 43200) + " months"; | |
if (distance_in_minutes < 1051199) return "about 1 year"; | |
return "over " + (distance_in_minutes / 525960).floor() + " years"; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment