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'),
|
2018-01-09 16:50:57 +03:00
|
|
|
SafeString = proxy.SafeString,
|
|
|
|
i18n = proxy.i18n;
|
2017-04-04 19:07:35 +03:00
|
|
|
|
|
|
|
module.exports = function (date, options) {
|
2018-01-09 16:50:57 +03:00
|
|
|
var timezone, format, timeago, timeNow, dateMoment;
|
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;
|
2018-01-09 14:35:08 +03:00
|
|
|
timezone = options.data.blog.timezone;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2018-01-09 14:35:08 +03:00
|
|
|
// set to published_at by default, if it's available
|
|
|
|
// otherwise, this will print the current date
|
|
|
|
if (this.published_at) {
|
|
|
|
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;
|
2018-01-09 14:41:40 +03:00
|
|
|
timezone = options.data.blog.timezone;
|
2017-04-04 19:07:35 +03:00
|
|
|
timeNow = moment().tz(timezone);
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
// i18n: Making dates, including month names, translatable to any language.
|
|
|
|
// Documentation: http://momentjs.com/docs/#/i18n/
|
|
|
|
// Locales: https://github.com/moment/moment/tree/develop/locale
|
|
|
|
dateMoment = moment(date);
|
|
|
|
dateMoment.locale(i18n.locale());
|
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
if (timeago) {
|
2018-01-09 16:50:57 +03:00
|
|
|
date = timezone ? dateMoment.tz(timezone).from(timeNow) : dateMoment.fromNow();
|
2014-10-10 18:54:07 +04:00
|
|
|
} else {
|
2018-01-09 16:50:57 +03:00
|
|
|
date = timezone ? dateMoment.tz(timezone).format(format) : dateMoment.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
|
|
|
};
|