Skip to content

Instantly share code, notes, and snippets.

@wtfaremyinitials
Created September 25, 2014 19:00
Show Gist options
  • Save wtfaremyinitials/9d72b3ade7abba3154ae to your computer and use it in GitHub Desktop.
Save wtfaremyinitials/9d72b3ade7abba3154ae to your computer and use it in GitHub Desktop.
Meteor timeAgo
/*
* Meteor timeAgo helper
* by
* MIT License
*/
UI.registerHelper('formatTimeAgo', function(timestamp, options) {
var minute = 60;
var hour = minute * 60;
var day = hour * 24;
var week = day * 7;
var month = week * 4;
var year = month * 12;
var seconds = timestamp;
var secondsAgo = getCurrentTime() - seconds;
if(secondsAgo < 5)
return 'just now';
if(secondsAgo < minute)
return 'less than 1 minute ago';
if(secondsAgo < hour)
return Math.floor(secondsAgo / minute) + ' minutes ago';
if(secondsAgo < day)
return Math.floor(secondsAgo / hour) + ' hours ago';
if(secondsAgo < week)
return Math.floor(secondsAgo / day) + ' days ago';
if(secondsAgo < month)
return Math.floor(secondsAgo / week) + ' weeks ago';
if(secondsAgo < year)
return Math.floor(secondsAgo / month) + ' months ago';
return Math.floor(secondsAgo / year) + ' years ago';
});
var currentTimeDep = new Deps.Dependency;
var getCurrentTime = function() {
currentTimeDep.depend();
return Math.floor(Date.now() / 1000);
};
Meteor.setInterval(function() {
currentTimeDep.changed();
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment