From 8ffb742d83a056e0509b8da3544a4845209e07eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20G=C3=B3mez=20Vilches?= Date: Thu, 19 Jan 2017 17:37:50 +0100 Subject: [PATCH 1/2] Allow display moment DateTime in its own timezone --- backgrid-moment-cell.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/backgrid-moment-cell.js b/backgrid-moment-cell.js index 8045fea..b988fd8 100644 --- a/backgrid-moment-cell.js +++ b/backgrid-moment-cell.js @@ -39,6 +39,7 @@ var MomentFormatter = exports.MomentFormatter = Backgrid.Extension.MomentFormatter = function (options) { _.extend(this, this.defaults, options); }; + var useLocale = "locale" in moment && _.isFunction(moment, "locale"); MomentFormatter.prototype = new Backgrid.CellFormatter; _.extend(MomentFormatter.prototype, { @@ -83,12 +84,13 @@ modelInUnixOffset: false, modelInUnixTimestamp: false, modelInUTC: true, - modelLang: moment.locale(), + modelLang: useLocale ? moment.locale() : moment.lang(), modelFormat: moment.defaultFormat, displayInUnixOffset: false, displayInUnixTimestamp: false, displayInUTC: true, - displayLang: moment.locale(), + displayInTimezone: false, + displayLang: useLocale ? moment.locale() : moment.lang(), displayFormat: moment.defaultFormat }, @@ -112,15 +114,13 @@ if (this.displayInUnixTimestamp) return m.unix(); - if (this.displayLang) m.locale(this.displayLang); - - if (this.displayInUTC) m.utc(); else m.local(); - - if (this.displayFormat != moment.defaultFormat) { - return m.format(this.displayFormat); + if (this.displayLang) { + if (useLocale) m.locale(this.displayLang); else m.lang(this.displayLang); } - return m.format(); + if (this.displayInUTC) m.utc(); else if (this.displayInTimezone) m.parseZone(); else m.local(); + + return m.format(this.displayFormat); }, /** @@ -144,15 +144,13 @@ if (this.modelInUnixTimestamp) return m.unix(); - if (this.modelLang) m.locale(this.modelLang); - - if (this.modelInUTC) m.utc(); else m.local() - - if (this.modelFormat != moment.defaultFormat) { - return m.format(this.modelFormat); + if (this.modelLang) { + if (useLocale) m.locale(this.modelLang); else m.lang(this.modelLang); } - return m.format(); + if (this.modelInUTC) m.utc(); else m.local(); + + return m.format(this.modelFormat); } }); From 211e19a0bced3ec8d814ec42ecd0c3e87edfc0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20G=C3=B3mez=20Vilches?= Date: Thu, 19 Jan 2017 17:41:41 +0100 Subject: [PATCH 2/2] Update backgrid-moment-cell.js --- backgrid-moment-cell.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/backgrid-moment-cell.js b/backgrid-moment-cell.js index b988fd8..89f9555 100644 --- a/backgrid-moment-cell.js +++ b/backgrid-moment-cell.js @@ -39,7 +39,6 @@ var MomentFormatter = exports.MomentFormatter = Backgrid.Extension.MomentFormatter = function (options) { _.extend(this, this.defaults, options); }; - var useLocale = "locale" in moment && _.isFunction(moment, "locale"); MomentFormatter.prototype = new Backgrid.CellFormatter; _.extend(MomentFormatter.prototype, { @@ -84,13 +83,13 @@ modelInUnixOffset: false, modelInUnixTimestamp: false, modelInUTC: true, - modelLang: useLocale ? moment.locale() : moment.lang(), + modelLang: moment.locale(), modelFormat: moment.defaultFormat, displayInUnixOffset: false, displayInUnixTimestamp: false, displayInUTC: true, displayInTimezone: false, - displayLang: useLocale ? moment.locale() : moment.lang(), + displayLang: moment.locale(), displayFormat: moment.defaultFormat }, @@ -114,13 +113,15 @@ if (this.displayInUnixTimestamp) return m.unix(); - if (this.displayLang) { - if (useLocale) m.locale(this.displayLang); else m.lang(this.displayLang); - } + if (this.displayLang) m.locale(this.displayLang); if (this.displayInUTC) m.utc(); else if (this.displayInTimezone) m.parseZone(); else m.local(); - return m.format(this.displayFormat); + if (this.displayFormat != moment.defaultFormat) { + return m.format(this.displayFormat); + } + + return m.format(); }, /** @@ -144,13 +145,15 @@ if (this.modelInUnixTimestamp) return m.unix(); - if (this.modelLang) { - if (useLocale) m.locale(this.modelLang); else m.lang(this.modelLang); - } + if (this.modelLang) m.locale(this.modelLang); + + if (this.modelInUTC) m.utc(); else m.local() - if (this.modelInUTC) m.utc(); else m.local(); + if (this.modelFormat != moment.defaultFormat) { + return m.format(this.modelFormat); + } - return m.format(this.modelFormat); + return m.format(); } });