diff --git a/core/src/components/datetime/datetime-util.ts b/core/src/components/datetime/datetime-util.ts index c7a540e8e4..bd8ff7ab45 100644 --- a/core/src/components/datetime/datetime-util.ts +++ b/core/src/components/datetime/datetime-util.ts @@ -248,6 +248,17 @@ export function parseDate(val: string | undefined | null): DatetimeData | undefi * such as "01:47" */ export const getLocalDateTime = (dateString: any = ''): Date => { + /** + * If user passed in undefined + * or null, convert it to the + * empty string since the rest + * of this functions expects + * a string + */ + if (dateString === undefined || dateString === null) { + dateString = ''; + } + /** * Ensures that YYYY-MM-DD, YYYY-MM, * YYYY-DD, etc does not get affected diff --git a/core/src/components/datetime/test/datetime.spec.ts b/core/src/components/datetime/test/datetime.spec.ts index f099ae7ce4..6f0e21719f 100644 --- a/core/src/components/datetime/test/datetime.spec.ts +++ b/core/src/components/datetime/test/datetime.spec.ts @@ -1,4 +1,4 @@ -import { DatetimeData, daysInMonth, getDateValue, getLocalDateTime } from '../datetime-util'; +import { DatetimeData, daysInMonth, getDateValue, getLocalDateTime, renderDatetime } from '../datetime-util'; describe('Datetime', () => { describe('getDateValue()', () => { @@ -69,6 +69,17 @@ describe('Datetime', () => { expect(convertToLocal.toISOString()).toContain(test.expectedOutput); }); }); + + it('should default to today for null and undefined cases', () => { + const today = new Date(); + const todayString = renderDatetime('YYYY-MM-DD', { year: today.getFullYear(), month: today.getMonth() + 1, day: today.getDate() } ) + + const convertToLocalUndefined = getLocalDateTime(undefined); + expect(convertToLocalUndefined.toISOString()).toContain(todayString); + + const convertToLocalNull = getLocalDateTime(null); + expect(convertToLocalNull.toISOString()).toContain(todayString); + }); }); describe('daysInMonth()', () => {