Created
August 13, 2012 14:20
-
-
Save micho/3341189 to your computer and use it in GitHub Desktop.
posted_at relative time
This file contains 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
/*global I18n*/ | |
(function ($) { | |
var PostedAt = {} | |
, moment = this.moment; | |
// Format dates in a human way. Read the code. | |
moment.fn.humanFormat = function () { | |
// ensure that we are on utc | |
var utc = this.clone().utc() | |
, now = _.date(Date.now()).utc(); | |
if (utc > now.clone().subtract('minutes', 1)) { | |
return I18n.translations.date.now; | |
} else if (utc > now.clone().subtract('hours', 1)) { | |
return this.from(now); | |
} else if (utc > now.clone().sod()) { | |
return this.local().format("h:mm a"); | |
} else if (utc > now.clone().sod().subtract('days', 1)) { | |
return I18n.translations.date.yesterday + " " + this.local().format("h:mm a"); | |
} else if (utc > now.clone().sod().subtract('days', 6)) { | |
return this.local().format("ddd h a"); | |
} else if (utc.year() === now.clone().year() && utc.month() === now.clone().month()) { | |
return this.local().format("MMM Do h a"); | |
} else if (utc.year() === now.clone().year()) { | |
return this.local().format("MMM Do"); | |
} else { | |
return this.local().format("MMM Do YYYY"); | |
} | |
}; | |
// Replaces all the cache-friendly static dates | |
// with relative time ago in words | |
PostedAt.format_posted_dates = function () { | |
$('time.timeago').each(function () { | |
$(this).html(_.date(+$(this).attr('data-msec')).humanFormat()); | |
}); | |
}; | |
setInterval(PostedAt.format_posted_dates, 30000); | |
//exports | |
Teambox.modules.posted_at = PostedAt; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment