2014-10-10 15:54:07 +01:00
|
|
|
// # Date Helper
|
|
|
|
// Usage: `{{date format="DD MM, YYYY"}}`, `{{date updated_at format="DD MM, YYYY"}}`
|
|
|
|
//
|
2016-02-02 09:04:40 +02: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 15:54:07 +01:00
|
|
|
|
2020-04-08 16:56:37 +01:00
|
|
|
const {SafeString, themeI18n} = require('../services/proxy');
|
2019-04-02 01:36:13 -05:00
|
|
|
const moment = require('moment-timezone');
|
2017-04-04 17:07:35 +01:00
|
|
|
|
|
|
|
module.exports = function (date, options) {
|
2019-04-02 01:36:13 -05:00
|
|
|
let timezone;
|
2014-10-10 15:54:07 +01:00
|
|
|
|
2019-08-08 10:47:13 +02:00
|
|
|
if (!options && Object.prototype.hasOwnProperty.call(date, 'hash')) {
|
2016-02-21 18:48:44 +00:00
|
|
|
options = date;
|
|
|
|
date = undefined;
|
2019-09-10 11:37:04 +02:00
|
|
|
timezone = options.data.site.timezone;
|
2014-10-10 15:54:07 +01:00
|
|
|
|
2018-01-09 18:35:08 +07: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 15:54:07 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 01:36:13 -05:00
|
|
|
const {
|
|
|
|
format = 'MMM DD, YYYY',
|
|
|
|
timeago
|
|
|
|
} = options.hash;
|
|
|
|
|
2014-10-10 15:54:07 +01:00
|
|
|
// ensure that context is undefined, not null, as that can cause errors
|
2016-02-21 18:48:44 +00:00
|
|
|
date = date === null ? undefined : date;
|
2019-09-10 11:37:04 +02:00
|
|
|
timezone = options.data.site.timezone;
|
2019-04-02 01:36:13 -05:00
|
|
|
const timeNow = moment().tz(timezone);
|
2014-10-10 15:54:07 +01:00
|
|
|
|
2018-01-09 14:50:57 +01: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 01:36:13 -05:00
|
|
|
const dateMoment = moment(date);
|
2020-03-19 14:07:20 +00:00
|
|
|
dateMoment.locale(themeI18n.locale());
|
2018-01-09 14:50:57 +01:00
|
|
|
|
2014-10-10 15:54:07 +01:00
|
|
|
if (timeago) {
|
2018-01-09 14:50:57 +01:00
|
|
|
date = timezone ? dateMoment.tz(timezone).from(timeNow) : dateMoment.fromNow();
|
2014-10-10 15:54:07 +01:00
|
|
|
} else {
|
2018-01-09 14:50:57 +01:00
|
|
|
date = timezone ? dateMoment.tz(timezone).format(format) : dateMoment.format(format);
|
2014-10-10 15:54:07 +01:00
|
|
|
}
|
2016-02-02 09:04:40 +02:00
|
|
|
|
2017-04-04 17:07:35 +01:00
|
|
|
return new SafeString(date);
|
2014-10-10 15:54:07 +01:00
|
|
|
};
|