From 650519683c4814b6f89b096206d000bc3c476999 Mon Sep 17 00:00:00 2001 From: lindhork <45235524+lindhork@users.noreply.github.com> Date: Thu, 11 Feb 2021 03:54:12 +0100 Subject: [PATCH] Added compatibility for single digit hours in 24 hours format --- client/components/cards/cardDate.js | 13 ++++++++----- client/lib/datepicker.js | 13 ++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/client/components/cards/cardDate.js b/client/components/cards/cardDate.js index aa73439bd..0cab46540 100644 --- a/client/components/cards/cardDate.js +++ b/client/components/cards/cardDate.js @@ -1,3 +1,8 @@ +// Helper function to replace HH with H for 24 hours format, because H allows also single-digit hours +function adjustedTimeFormat() { + return moment.localeData().longDateFormat('LT').replace(/HH/i, 'H'); +} + // Edit received, start, due & end dates BlazeComponent.extendComponent({ template() { @@ -59,7 +64,7 @@ BlazeComponent.extendComponent({ }, 'keyup .js-time-field'() { // parse for localized time format in strict mode - const dateMoment = moment(this.find('#time').value, 'LT', true); + const dateMoment = moment(this.find('#time').value, adjustedTimeFormat(), true); if (dateMoment.isValid()) { this.error.set(''); } @@ -71,15 +76,13 @@ BlazeComponent.extendComponent({ const time = evt.target.time.value || moment(new Date().setHours(12, 0, 0)).format('LT'); - const dateString = `${evt.target.date.value} ${time}`; - const newDate = moment(dateString, 'L LT', true); + const newDate = moment(dateString, 'L ' + adjustedTimeFormat(), true); if (newDate.isValid()) { this._storeDate(newDate.toDate()); Popup.close(); } else { - this.error.set('invalid-date'); - evt.target.date.focus(); + this.error.set('invalid'); } }, 'click .js-delete-date'(evt) { diff --git a/client/lib/datepicker.js b/client/lib/datepicker.js index aa05310c2..d415fde39 100644 --- a/client/lib/datepicker.js +++ b/client/lib/datepicker.js @@ -1,3 +1,8 @@ +// Helper function to replace HH with H for 24 hours format, because H allows also single-digit hours +function adjustedTimeFormat() { + return moment.localeData().longDateFormat('LT').replace(/HH/i, 'H'); +} + DatePicker = BlazeComponent.extendComponent({ template() { return 'datepicker'; @@ -77,7 +82,7 @@ DatePicker = BlazeComponent.extendComponent({ }, 'keyup .js-time-field'() { // parse for localized time format in strict mode - const dateMoment = moment(this.find('#time').value, 'LT', true); + const dateMoment = moment(this.find('#time').value, adjustedTimeFormat(), true); if (dateMoment.isValid()) { this.error.set(''); } @@ -89,15 +94,13 @@ DatePicker = BlazeComponent.extendComponent({ const time = evt.target.time.value || moment(new Date().setHours(12, 0, 0)).format('LT'); - const dateString = `${evt.target.date.value} ${time}`; - const newDate = moment(dateString, 'L LT', true); + const newDate = moment(dateString, 'L ' + adjustedTimeFormat(), true); if (newDate.isValid()) { this._storeDate(newDate.toDate()); Popup.close(); } else { - this.error.set('invalid-date'); - evt.target.date.focus(); + this.error.set('invalid'); } }, 'click .js-delete-date'(evt) {