mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
fix(datetime): default to current date when value is null (#18105)
fixes #18099
This commit is contained in:
@@ -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()', () => {
|
||||
|
||||
Reference in New Issue
Block a user