Skip to content

Instantly share code, notes, and snippets.

@wolthers
Last active June 24, 2016 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wolthers/9227398 to your computer and use it in GitHub Desktop.
Save wolthers/9227398 to your computer and use it in GitHub Desktop.
Handlebars helper for converting unix timestamp in seconds to pretty date - "25 seconds ago" / "3 minutes ago" / "2 hours ago" / "2 days ago" / "December 25"
/**
@param {UNIX timestamp} timestamp - an unix timestamp in seconds
*/
Handlebars.registerHelper("prettyDate", function (timestamp) {
var date,
monthNames,
secs = ((new Date()).getTime() / 1000) - timestamp,
minutes = secs / 60,
hours = minutes / 60,
days = hours / 24,
weeks = days / 7,
months = weeks / 4.34812,
years = months / 12;
if (minutes < 1) {
secs = Math.floor(secs % 60);
return secs + (secs > 1 ? " seconds ago" : " second ago");
}
if (hours < 1) {
hours = Math.floor(minutes % 60);
return hours + (minutes > 1 ? " minutes ago" : " minute ago");
}
if (days < 1) {
hours = Math.floor(hours % 24);
return hours + (hours > 1 ? " hours ago" : " hour ago");
}
else if (days < 4) {
days = Math.floor(days % 7);
return days + (days > 1 ? " days ago" : " day ago");
}
else {
date = new Date(timestamp * 1000);
monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
return monthNames[date.getMonth()] + " " + date.getDate();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment