Skip to content

Commit

Permalink
fix(datetime): default to current date when value is null (#18105)
Browse files Browse the repository at this point in the history
fixes #18099
  • Loading branch information
liamdebeasi committed Apr 23, 2019
1 parent 464ec3b commit ca233b5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 11 additions & 0 deletions core/src/components/datetime/datetime-util.ts
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion 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()', () => {
Expand Down Expand Up @@ -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()', () => {
Expand Down

0 comments on commit ca233b5

Please sign in to comment.