diff --git a/src/material-date-fns-adapter/adapter/date-fns-adapter.spec.ts b/src/material-date-fns-adapter/adapter/date-fns-adapter.spec.ts index d85b32e8341d..aa4973e3e419 100644 --- a/src/material-date-fns-adapter/adapter/date-fns-adapter.spec.ts +++ b/src/material-date-fns-adapter/adapter/date-fns-adapter.spec.ts @@ -535,6 +535,15 @@ describe('DateFnsAdapter', () => { expect(adapter.getSeconds(result)).toBe(0); }); + it('should parse a time string containing only hours', () => { + const result = adapter.parseTime('11', 'HH')!; + expect(result).toBeTruthy(); + expect(adapter.isValid(result)).toBe(true); + expect(adapter.getHours(result)).toBe(11); + expect(adapter.getMinutes(result)).toBe(0); + expect(adapter.getSeconds(result)).toBe(0); + }); + it('should return an invalid date when parsing invalid time string', () => { expect(adapter.isValid(adapter.parseTime('abc', 'p')!)).toBe(false); expect(adapter.isValid(adapter.parseTime('123', 'p')!)).toBe(false); diff --git a/src/material-date-fns-adapter/adapter/date-fns-adapter.ts b/src/material-date-fns-adapter/adapter/date-fns-adapter.ts index 91e37fb1cc2d..3201880f33dc 100644 --- a/src/material-date-fns-adapter/adapter/date-fns-adapter.ts +++ b/src/material-date-fns-adapter/adapter/date-fns-adapter.ts @@ -162,35 +162,7 @@ export class DateFnsAdapter extends DateAdapter { } parse(value: unknown, parseFormat: string | string[]): Date | null { - if (typeof value == 'string' && value.length > 0) { - const iso8601Date = parseISO(value); - - if (this.isValid(iso8601Date)) { - return iso8601Date; - } - - const formats = Array.isArray(parseFormat) ? parseFormat : [parseFormat]; - - if (!parseFormat.length) { - throw Error('Formats array must not be empty.'); - } - - for (const currentFormat of formats) { - const fromFormat = parse(value, currentFormat, new Date(), {locale: this.locale}); - - if (this.isValid(fromFormat)) { - return fromFormat; - } - } - - return this.invalid(); - } else if (typeof value === 'number') { - return new Date(value); - } else if (value instanceof Date) { - return this.clone(value); - } - - return null; + return this._parse(value, parseFormat); } format(date: Date, displayFormat: string): string { @@ -278,10 +250,48 @@ export class DateFnsAdapter extends DateAdapter { } override parseTime(value: unknown, parseFormat: string | string[]): Date | null { - return this.parse(value, parseFormat); + return this._parse(value, parseFormat, false); } override addSeconds(date: Date, amount: number): Date { return addSeconds(date, amount); } + + private _parse( + value: unknown, + parseFormat: string | string[], + shouldTryParseIso = true, + ): Date | null { + if (typeof value == 'string' && value.length > 0) { + if (shouldTryParseIso) { + const iso8601Date = parseISO(value); + + if (this.isValid(iso8601Date)) { + return iso8601Date; + } + } + + const formats = Array.isArray(parseFormat) ? parseFormat : [parseFormat]; + + if (!parseFormat.length) { + throw Error('Formats array must not be empty.'); + } + + for (const currentFormat of formats) { + const fromFormat = parse(value, currentFormat, new Date(), {locale: this.locale}); + + if (this.isValid(fromFormat)) { + return fromFormat; + } + } + + return this.invalid(); + } else if (typeof value === 'number') { + return new Date(value); + } else if (value instanceof Date) { + return this.clone(value); + } + + return null; + } } diff --git a/src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts b/src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts index 5eed39517609..962ea66ee11f 100644 --- a/src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts +++ b/src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts @@ -633,6 +633,15 @@ describe('LuxonDateAdapter', () => { expect(adapter.getSeconds(result)).toBe(0); }); + it('should parse a time string containing only hours', () => { + const result = adapter.parseTime('11', 'HH')!; + expect(result).toBeTruthy(); + expect(adapter.isValid(result)).toBe(true); + expect(adapter.getHours(result)).toBe(11); + expect(adapter.getMinutes(result)).toBe(0); + expect(adapter.getSeconds(result)).toBe(0); + }); + it('should parse a time string with characters around the time', () => { adapter.setLocale('bg-BG'); const result = adapter.parseTime('14:52 ч.', 't')!; diff --git a/src/material-moment-adapter/adapter/moment-date-adapter.spec.ts b/src/material-moment-adapter/adapter/moment-date-adapter.spec.ts index dffc542d3c26..c53365209bb6 100644 --- a/src/material-moment-adapter/adapter/moment-date-adapter.spec.ts +++ b/src/material-moment-adapter/adapter/moment-date-adapter.spec.ts @@ -614,6 +614,15 @@ describe('MomentDateAdapter', () => { expect(adapter.getSeconds(result)).toBe(0); }); + it('should parse a time string containing only hours', () => { + const result = adapter.parseTime('11', 'HH')!; + expect(result).toBeTruthy(); + expect(adapter.isValid(result)).toBe(true); + expect(adapter.getHours(result)).toBe(11); + expect(adapter.getMinutes(result)).toBe(0); + expect(adapter.getSeconds(result)).toBe(0); + }); + it('should parse a time string with characters around the time', () => { adapter.setLocale('bg-BG'); const result = adapter.parseTime('14:52 ч.', 'LT')!;