From 210db5b265c04dcdc847f173503c5c096fdb3374 Mon Sep 17 00:00:00 2001 From: Brandy Carney Date: Thu, 1 Oct 2020 13:58:20 -0400 Subject: [PATCH] fix(datetime): do not set ampm when the column doesn't exist (#22224) --- .../components/datetime/datetime-util.spec.ts | 8 ++--- core/src/components/datetime/datetime-util.ts | 11 +++++-- .../components/datetime/test/basic/index.html | 9 ++++-- .../components/datetime/test/datetime.spec.ts | 32 +++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/core/src/components/datetime/datetime-util.spec.ts b/core/src/components/datetime/datetime-util.spec.ts index db6dfd25ff..589b1b0bc9 100644 --- a/core/src/components/datetime/datetime-util.spec.ts +++ b/core/src/components/datetime/datetime-util.spec.ts @@ -220,7 +220,7 @@ describe('datetime-util', () => { "second": undefined, "tzOffset": 0, "year": 1000, - "ampm": "am" + "ampm": undefined }); }); @@ -235,7 +235,7 @@ describe('datetime-util', () => { "second": undefined, "tzOffset": 0, "year": undefined, - "ampm": "pm" + "ampm": undefined }); }); @@ -250,7 +250,7 @@ describe('datetime-util', () => { "second": 20, "tzOffset": 0, "year": 1994, - "ampm": "pm" + "ampm": undefined }); }); @@ -265,7 +265,7 @@ describe('datetime-util', () => { "second": undefined, "tzOffset": 0, "year": 2018, - "ampm": "am" + "ampm": undefined }); }); diff --git a/core/src/components/datetime/datetime-util.ts b/core/src/components/datetime/datetime-util.ts index d88a912b01..8b1c8785d6 100644 --- a/core/src/components/datetime/datetime-util.ts +++ b/core/src/components/datetime/datetime-util.ts @@ -3,10 +3,16 @@ * Defaults to the current date if * no date given */ -export const getDateValue = (date: DatetimeData, format: string): number => { +export const getDateValue = (date: DatetimeData, format: string): number | string => { const getValue = getValueFromFormat(date, format); - if (getValue !== undefined) { return getValue; } + if (getValue !== undefined) { + if (format === FORMAT_A || format === FORMAT_a) { + date.ampm = getValue; + } + + return getValue; + } const defaultDate = parseDate(new Date().toISOString()); return getValueFromFormat((defaultDate as DatetimeData), format); @@ -238,7 +244,6 @@ export const parseDate = (val: string | undefined | null): DatetimeData | undefi second: parse[6], millisecond: parse[7], tzOffset, - ampm: parse[4] >= 12 ? 'pm' : 'am' }; }; diff --git a/core/src/components/datetime/test/basic/index.html b/core/src/components/datetime/test/basic/index.html index 732271affe..aba8faa9ec 100644 --- a/core/src/components/datetime/test/basic/index.html +++ b/core/src/components/datetime/test/basic/index.html @@ -105,8 +105,8 @@ - HH:mm - + HH:mm A + @@ -119,6 +119,11 @@ + + h:mm A + + + hh:mm A (15 min steps) diff --git a/core/src/components/datetime/test/datetime.spec.ts b/core/src/components/datetime/test/datetime.spec.ts index a100755e1e..d151e7af41 100644 --- a/core/src/components/datetime/test/datetime.spec.ts +++ b/core/src/components/datetime/test/datetime.spec.ts @@ -30,6 +30,38 @@ describe('Datetime', () => { expect(monthvalue).toEqual(date.getMonth() + 1); expect(yearValue).toEqual(date.getFullYear()); }); + + it('it should return the date value for a given time', () => { + const dateTimeData: DatetimeData = { + hour: 2, + minute: 23, + tzOffset: 0 + }; + + const hourValue = getDateValue(dateTimeData, 'hh'); + const minuteValue = getDateValue(dateTimeData, 'mm'); + const ampmValue = getDateValue(dateTimeData, 'A'); + + expect(hourValue).toEqual(2); + expect(minuteValue).toEqual(23); + expect(ampmValue).toEqual("am"); + }); + + it('it should return the date value for a given time after 12', () => { + const dateTimeData: DatetimeData = { + hour: 16, + minute: 47, + tzOffset: 0 + }; + + const hourValue = getDateValue(dateTimeData, 'hh'); + const minuteValue = getDateValue(dateTimeData, 'mm'); + const ampmValue = getDateValue(dateTimeData, 'a'); + + expect(hourValue).toEqual(4); + expect(minuteValue).toEqual(47); + expect(ampmValue).toEqual("pm"); + }); }); describe('getLocalDateTime()', () => {