2014-10-10 18:54:07 +04:00
|
|
|
// # Date Helper
|
|
|
|
// Usage: `{{date format="DD MM, YYYY"}}`, `{{date updated_at format="DD MM, YYYY"}}`
|
|
|
|
//
|
2016-02-02 10:04:40 +03: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 18:54:07 +04:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
|
|
|
moment = require('moment-timezone'),
|
|
|
|
SafeString = proxy.SafeString;
|
|
|
|
|
|
|
|
module.exports = function (date, options) {
|
|
|
|
var timezone, format, timeago, timeNow;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2016-02-21 21:48:44 +03:00
|
|
|
if (!options && date.hasOwnProperty('hash')) {
|
|
|
|
options = date;
|
|
|
|
date = undefined;
|
2016-02-02 10:04:40 +03:00
|
|
|
timezone = options.data.blog.timezone;
|
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-02 10:04:40 +03:00
|
|
|
date = moment(this.published_at).tz(timezone).format();
|
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
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
format = options.hash.format || 'MMM DD, YYYY';
|
|
|
|
timeago = options.hash.timeago;
|
|
|
|
timeNow = moment().tz(timezone);
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
if (timeago) {
|
2017-04-04 19:07:35 +03:00
|
|
|
date = timezone ? moment(date).tz(timezone).from(timeNow) : moment(date).fromNow();
|
2014-10-10 18:54:07 +04:00
|
|
|
} else {
|
2017-04-04 19:07:35 +03:00
|
|
|
date = timezone ? moment(date).tz(timezone).format(format) : moment(date).format(format);
|
2014-10-10 18:54:07 +04:00
|
|
|
}
|
2016-02-02 10:04:40 +03:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(date);
|
2014-10-10 18:54:07 +04:00
|
|
|
};
|