Skip to content

Instantly share code, notes, and snippets.

@gregtap
Created November 6, 2015 03:36
Show Gist options
  • Select an option

  • Save gregtap/c0c4054728435759728e to your computer and use it in GitHub Desktop.

Select an option

Save gregtap/c0c4054728435759728e to your computer and use it in GitHub Desktop.
export default Component.extend({
tagName: 'time',
tick: function() {
this.nextTick = run.later(this, function() {
this.notifyPropertyChange('timeago');
this.tick();
}, 1000);
}.on('init'),
// Compare message date to current. If it has been less than 2 hours ago
// we use relative time otherwise we show the day + time.
timeAgoString: function() {
var now = new Date();
var date = this.get('date');
// Handles the slight out of sync clocks than can result in
// showing `in a few seconds` instead of `a few seconds ago`.
date = date > now ? now : date;
var elapsedTime = now - date;
if (elapsedTime < TWO_HOURS) {
return moment(date).fromNow();
}
if (elapsedTime < TWO_DAYS) {
return moment(date).format('ddd, h:m A');
}
return moment(date).format('MMM, DD h:m A');
}.property('timeago'),
willDestroyElement: function() {
run.cancel(this.get('nextTick'));
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment