Skip to content

Instantly share code, notes, and snippets.

@levi
Created August 29, 2010 01:58
Show Gist options
  • Save levi/555844 to your computer and use it in GitHub Desktop.
Save levi/555844 to your computer and use it in GitHub Desktop.
/**
Takes the created date and returns a fuzzy time string.
ie. 2 hours ago, 3 weeks ago.
@property
*/
prettyDate: function() {
var created = this.get('created'),
createdMS = created.get('milliseconds'),
now = SC.DateTime.create().get('milliseconds'),
diff = Math.floor((now - createdMS) / 1000),
dayDiff = Math.floor(diff / 86400);
if (dayDiff < 0 || dayDiff >= 31) {
return created.toFormattedString('%d %B %Y');
}
return dayDiff === 0 && (
diff < 60 && 'just now' ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') ||
dayDiff === 1 && "Yesterday" ||
dayDiff < 7 && dayDiff + ' days ago' ||
dayDiff < 31 && Math.ceil( dayDiff / 7 ) + ' weeks ago';
}.property('created').cacheable(),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment