Skip to content

Instantly share code, notes, and snippets.

@JerryC8080
Created December 16, 2014 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JerryC8080/39b11ce58b561368159e to your computer and use it in GitHub Desktop.
Save JerryC8080/39b11ce58b561368159e to your computer and use it in GitHub Desktop.
ghost date helper
// # Date Helper
// Usage: `{{date format="DD MM, YYYY"}}`, `{{date updated_at format="DD MM, YYYY"}}`
//
// Formats a date using moment.js. Formats published_at by default but will also take a date as a parameter
var moment = require('moment'),
date;
date = function (context, options) {
if (!options && context.hasOwnProperty('hash')) {
options = context;
context = undefined;
// set to published_at by default, if it's available
// otherwise, this will print the current date
if (this.published_at) {
context = this.published_at;
}
}
// ensure that context is undefined, not null, as that can cause errors
context = context === null ? undefined : context;
moment.lang("zh-cn");
var f = options.hash.format || 'MMM Do, YYYY',
timeago = options.hash.timeago,
date;
if (timeago) {
date = moment(context).fromNow();
} else {
date = moment(context).format(f);
}
moment.lang("en");
return date;
};
module.exports = date;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment