Created
November 6, 2015 03:36
-
-
Save gregtap/c0c4054728435759728e to your computer and use it in GitHub Desktop.
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
| 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