Last active
October 12, 2015 07:48
-
-
Save shuhaowu/3994233 to your computer and use it in GitHub Desktop.
Relative time helper like twitter in JavaScript
This file contains hidden or 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 relative_time = function(timestamp, relative_to) { | |
if (timestamp === null || timestamp === undefined) | |
return ""; | |
var delta, relative_is_future, t; | |
if (!relative_to) | |
relative_to = parseInt(new Date().getTime() / 1000); | |
if ($.type(timestamp) !== "number") | |
timestamp = timestamp.getTime() / 1000; | |
delta = timestamp - relative_to; | |
relative_is_future = delta > 0 ? true : false; | |
delta = Math.abs(delta); | |
if (delta < 60) { | |
t = "less than a minute"; | |
} else if (delta < 120) { | |
t = "about a minute"; | |
} else if (delta < 2700) { | |
t = (parseInt(delta / 60)).toString() + " minutes"; | |
} else if (delta < 5400) { | |
t = "about an hour"; | |
} else if (delta < 86400) { | |
t = "about " + (parseInt(delta / 3600)).toString() + " hours"; | |
} else if (delta < 172800) { | |
t = "one day"; | |
} else { | |
t = (parseInt(delta / 86400)).toString() + " days"; | |
} | |
if (relative_is_future) { | |
return "in " + t; | |
} else { | |
return t + " ago"; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment