Skip to content
Permalink
Browse files

fix(datetime): default to current date when value is null (#18105)

fixes #18099
  • Loading branch information...
liamdebeasi committed Apr 23, 2019
1 parent 464ec3b commit ca233b547af491489002d7e5391533fe2424a6dc
@@ -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
@@ -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()', () => {

0 comments on commit ca233b5

Please sign in to comment.
You can’t perform that action at this time.