2014-10-10 09:54:07 -05:00
|
|
|
// # Date Helper
|
|
|
|
// Usage: `{{date format="DD MM, YYYY"}}`, `{{date updated_at format="DD MM, YYYY"}}`
|
|
|
|
//
|
2016-02-02 02:04:40 -05:00
|
|
|
// Formats a date using moment-timezone.js. Formats published_at by default but will also take a date as a parameter
|
2014-10-10 09:54:07 -05:00
|
|
|
|
2016-10-31 08:44:24 -05:00
|
|
|
var moment = require('moment-timezone'),
|
2016-02-02 02:04:40 -05:00
|
|
|
date,
|
|
|
|
timezone;
|
2014-10-10 09:54:07 -05:00
|
|
|
|
2016-02-21 13:48:44 -05:00
|
|
|
date = function (date, options) {
|
|
|
|
if (!options && date.hasOwnProperty('hash')) {
|
|
|
|
options = date;
|
|
|
|
date = undefined;
|
2016-02-02 02:04:40 -05:00
|
|
|
timezone = options.data.blog.timezone;
|
2014-10-10 09:54:07 -05:00
|
|
|
|
|
|
|
// set to published_at by default, if it's available
|
|
|
|
// otherwise, this will print the current date
|
|
|
|
if (this.published_at) {
|
2016-02-02 02:04:40 -05:00
|
|
|
date = moment(this.published_at).tz(timezone).format();
|
2014-10-10 09:54:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that context is undefined, not null, as that can cause errors
|
2016-02-21 13:48:44 -05:00
|
|
|
date = date === null ? undefined : date;
|
2014-10-10 09:54:07 -05:00
|
|
|
|
2016-02-02 02:04:40 -05:00
|
|
|
var f = options.hash.format || 'MMM DD, YYYY',
|
|
|
|
timeago = options.hash.timeago,
|
|
|
|
timeNow = moment().tz(timezone);
|
2014-10-10 09:54:07 -05:00
|
|
|
|
|
|
|
if (timeago) {
|
2016-02-02 02:04:40 -05:00
|
|
|
date = timezone ? moment(date).tz(timezone).from(timeNow) : moment(date).fromNow();
|
2014-10-10 09:54:07 -05:00
|
|
|
} else {
|
2016-02-02 02:04:40 -05:00
|
|
|
date = timezone ? moment(date).tz(timezone).format(f) : moment(date).format(f);
|
2014-10-10 09:54:07 -05:00
|
|
|
}
|
2016-02-02 02:04:40 -05:00
|
|
|
|
2014-10-10 09:54:07 -05:00
|
|
|
return date;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = date;
|