From 11f34887d0b05ac29614af56950655fee9f0477c Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 17 Jul 2019 12:00:32 +0200 Subject: [PATCH] MOBILE-2808 datetime: Do not use am/pm in datetime --- src/addon/calendar/pages/event/event.ts | 6 ++--- .../mod/data/fields/date/component/date.ts | 6 ++--- .../datetime/component/datetime.ts | 6 ++--- src/providers/utils/time.ts | 23 +++++++++++++++++++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/addon/calendar/pages/event/event.ts b/src/addon/calendar/pages/event/event.ts index 0f7fed557..ece9ca7bf 100644 --- a/src/addon/calendar/pages/event/event.ts +++ b/src/addon/calendar/pages/event/event.ts @@ -72,9 +72,9 @@ export class AddonCalendarEventPage { this.defaultTime = defaultTime * 60; }); - // Calculate format to use. ion-datetime doesn't support escaping characters ([]), so we remove them. - this.notificationFormat = this.timeUtils.convertPHPToMoment(this.translate.instant('core.strftimedatetime')) - .replace(/[\[\]]/g, ''); + // Calculate format to use. + this.notificationFormat = this.timeUtils.fixFormatForDatetime(this.timeUtils.convertPHPToMoment( + this.translate.instant('core.strftimedatetime'))); } } diff --git a/src/addon/mod/data/fields/date/component/date.ts b/src/addon/mod/data/fields/date/component/date.ts index 187a6fbba..ff0ab6ded 100644 --- a/src/addon/mod/data/fields/date/component/date.ts +++ b/src/addon/mod/data/fields/date/component/date.ts @@ -42,9 +42,9 @@ export class AddonModDataFieldDateComponent extends AddonModDataFieldPluginCompo let val; - // Calculate format to use. ion-datetime doesn't support escaping characters ([]), so we remove them. - this.format = this.timeUtils.convertPHPToMoment(this.translate.instant('core.strftimedate')) - .replace(/[\[\]]/g, ''); + // Calculate format to use. + this.format = this.timeUtils.fixFormatForDatetime(this.timeUtils.convertPHPToMoment( + this.translate.instant('core.strftimedate'))); if (this.mode == 'search') { this.addControl('f_' + this.field.id + '_z'); diff --git a/src/addon/userprofilefield/datetime/component/datetime.ts b/src/addon/userprofilefield/datetime/component/datetime.ts index 1d9e9ea03..8da732a64 100644 --- a/src/addon/userprofilefield/datetime/component/datetime.ts +++ b/src/addon/userprofilefield/datetime/component/datetime.ts @@ -47,9 +47,9 @@ export class AddonUserProfileFieldDatetimeComponent implements OnInit { // Check if it's only date or it has time too. const hasTime = this.utils.isTrueOrOne(field.param3); - // Calculate format to use. ion-datetime doesn't support escaping characters ([]), so we remove them. - field.format = this.timeUtils.convertPHPToMoment(this.translate.instant('core.' + - (hasTime ? 'strftimedatetime' : 'strftimedate'))).replace(/[\[\]]/g, ''); + // Calculate format to use. + field.format = this.timeUtils.fixFormatForDatetime(this.timeUtils.convertPHPToMoment( + this.translate.instant('core.' + (hasTime ? 'strftimedatetime' : 'strftimedate')))); // Check min value. if (field.param1) { diff --git a/src/providers/utils/time.ts b/src/providers/utils/time.ts index e0c5633e1..f7c2de9f6 100644 --- a/src/providers/utils/time.ts +++ b/src/providers/utils/time.ts @@ -118,6 +118,29 @@ export class CoreTimeUtilsProvider { return converted; } + /** + * Fix format to use in an ion-datetime. + * + * @param {string} format Format to use. + * @return {string} Fixed format. + */ + fixFormatForDatetime(format: string): string { + if (!format) { + return ''; + } + + // The component ion-datetime doesn't support escaping characters ([]), so we remove them. + let fixed = format.replace(/[\[\]]/g, ''); + + if (fixed.indexOf('A') != -1) { + // Do not use am/pm format because there is a bug in ion-datetime. + fixed = fixed.replace(/ ?A/g, ''); + fixed = fixed.replace(/h/g, 'H'); + } + + return fixed; + } + /** * Returns hours, minutes and seconds in a human readable format *