2014-10-10 18:54:07 +04:00
|
|
|
// # 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;
|
|
|
|
|
2016-02-21 21:48:44 +03:00
|
|
|
date = function (date, options) {
|
|
|
|
if (!options && date.hasOwnProperty('hash')) {
|
|
|
|
options = date;
|
|
|
|
date = undefined;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
// set to published_at by default, if it's available
|
|
|
|
// otherwise, this will print the current date
|
|
|
|
if (this.published_at) {
|
2016-02-21 21:48:44 +03:00
|
|
|
date = this.published_at;
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that context is undefined, not null, as that can cause errors
|
2016-02-21 21:48:44 +03:00
|
|
|
date = date === null ? undefined : date;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
var f = options.hash.format || 'MMM Do, YYYY',
|
2016-02-21 21:48:44 +03:00
|
|
|
timeago = options.hash.timeago;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
if (timeago) {
|
2016-02-21 21:48:44 +03:00
|
|
|
date = moment(date).fromNow();
|
2014-10-10 18:54:07 +04:00
|
|
|
} else {
|
2016-02-21 21:48:44 +03:00
|
|
|
date = moment(date).format(f);
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
|
|
|
return date;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = date;
|