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
|
|
|
|
2020-04-08 18:56:37 +03:00
|
|
|
const {SafeString, themeI18n} = require('../services/proxy');
|
2019-04-02 09:36:13 +03:00
|
|
|
const moment = require('moment-timezone');
|
2017-04-04 19:07:35 +03:00
|
|
|
|
|
|
|
module.exports = function (date, options) {
|
2019-04-02 09:36:13 +03:00
|
|
|
let timezone;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2019-08-08 11:47:13 +03:00
|
|
|
if (!options && Object.prototype.hasOwnProperty.call(date, 'hash')) {
|
2016-02-21 21:48:44 +03:00
|
|
|
options = date;
|
|
|
|
date = undefined;
|
2019-09-10 12:37:04 +03:00
|
|
|
timezone = options.data.site.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
|
|
|
}
|
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
const {
|
|
|
|
format = 'MMM DD, YYYY',
|
|
|
|
timeago
|
|
|
|
} = options.hash;
|
|
|
|
|
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;
|
2019-09-10 12:37:04 +03:00
|
|
|
timezone = options.data.site.timezone;
|
2019-04-02 09:36:13 +03:00
|
|
|
const 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
|
2019-04-02 09:36:13 +03:00
|
|
|
const dateMoment = moment(date);
|
2020-03-19 17:07:20 +03:00
|
|
|
dateMoment.locale(themeI18n.locale());
|
2018-01-09 16:50:57 +03:00
|
|
|
|
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
|
|
|
};
|