Skip to content

Commit 509d304

Browse files
committed
Improve date formatting
1 parent e2a6313 commit 509d304

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

I18N.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ const Defaults = {
99
year: 'numeric'
1010
},
1111
dateTimeFormatterOptions: {
12+
day: 'numeric',
13+
month: 'numeric',
14+
year: 'numeric',
15+
hour: 'numeric',
16+
minute: 'numeric',
17+
second: 'numeric',
1218
timeZone: "UTC",
1319
timeZoneName: "short"
1420
}
@@ -23,19 +29,23 @@ const I18N = {
2329
numberFormatter: new Intl.NumberFormat(),
2430
translate: null, // function(value: string, vars: array|object = null) : string
2531

32+
getDefaults() {
33+
return Defaults;
34+
},
35+
2636
setLocales(locales, dateFormatterOptions = {}, dateTimeFormatterOptions = {}, numberFormatterOptions = {}, collatorOptions = {}) {
2737
this.locales = locales;
2838

29-
collatorOptions = Object.assign({}, this.collatorOptions, collatorOptions);
39+
collatorOptions = Object.assign({}, Defaults.collatorOptions, collatorOptions);
3040
this.collator = new Intl.Collator(locales, collatorOptions);
3141

32-
dateFormatterOptions = Object.assign({}, this.dateFormatterOptions, dateFormatterOptions);
42+
dateFormatterOptions = Object.assign({}, Defaults.dateFormatterOptions, dateFormatterOptions);
3343
this.dateFormatter = new Intl.DateTimeFormat(locales, dateFormatterOptions);
3444

35-
dateTimeFormatterOptions = Object.assign({}, this.dateTimeFormatterOptions, dateTimeFormatterOptions);
45+
dateTimeFormatterOptions = Object.assign({}, Defaults.dateTimeFormatterOptions, dateTimeFormatterOptions);
3646
this.dateTimeFormatter = new Intl.DateTimeFormat(locales, dateTimeFormatterOptions);
3747

38-
numberFormatterOptions = Object.assign({}, this.numberFormatterOptions, numberFormatterOptions);
48+
numberFormatterOptions = Object.assign({}, Defaults.numberFormatterOptions, numberFormatterOptions);
3949
this.numberFormatter = new Intl.NumberFormat(locales, numberFormatterOptions);
4050
},
4151

formatters.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -374,23 +374,36 @@ const Formatters = {
374374
}
375375
},
376376

377-
formatTemporalExtent(value, field, spec = {}) {
378-
let formatter = spec.shorten ? Formatters.formatDate : Formatters.formatTimestamp;
379-
if (!Array.isArray(value) || value.length < 2 || (typeof value[0] !== 'string' && typeof value[1] !== 'string')) {
377+
formatTemporalExtent(value, field, spec = {}) {
378+
if (!Array.isArray(value) || value.length !== 2) {
380379
return DataTypes.null();
381380
}
382-
else if (typeof value[0] !== 'string') {
383-
return _.t("Until {0}", [formatter(value[1])]);
384-
}
385-
else if (typeof value[1] !== 'string') {
386-
return _.t("{0} until present", [formatter(value[0])]);
387-
}
388-
else if (value[0] === value[1]) {
389-
return Formatters.formatTimestamp(value[0]);
390-
}
391-
else {
392-
return value.map(date => formatter(date)).join(' - ');
393-
}
381+
382+
value = value.map(d => {
383+
try {
384+
return d ? new Date(d) : null;
385+
} catch(e) {
386+
return null;
387+
}
388+
});
389+
390+
try {
391+
const [start, end] = value;
392+
if (start || end) {
393+
const base = spec.shorten ? I18N.dateFormatter : I18N.dateTimeFormatter;
394+
if (!start) {
395+
return _.t("Until {0}", [base.format(end)]);
396+
}
397+
else if (!end) {
398+
return _.t("{0} until present", [base.format(start)]);
399+
}
400+
else {
401+
return base.formatRange(start, end);
402+
}
403+
}
404+
} catch (error) {}
405+
406+
return DataTypes.null();
394407
},
395408

396409
formatTemporalExtents(value, field, spec = {}) {

0 commit comments

Comments
 (0)