|
1 | 1 | import { |
| 2 | + formatDate as ngFormatDate, |
2 | 3 | getLocaleCurrencyCode, |
3 | 4 | getLocaleDateFormat as ngGetLocaleDateFormat, |
4 | 5 | getLocaleDateTimeFormat as ngGetLocaleDateTimeFormat, |
@@ -681,29 +682,36 @@ export function getLocaleDateTimeFormat(locale: string, displayFormat?: string) |
681 | 682 | * coalescing to an empty string. |
682 | 683 | */ |
683 | 684 | export function formatDate(value: Date | string | number | null | undefined, format: string, locale: string, timezone?: string): string { |
684 | | - if (value === null || value === undefined || value === '') { |
685 | | - return ''; |
| 685 | + let formattedDate: string; |
| 686 | + try { |
| 687 | + formattedDate = ngFormatDate(value, format, locale, timezone); |
| 688 | + } catch { |
| 689 | + if (value === null || value === undefined || value === '') { |
| 690 | + return ''; |
| 691 | + } |
| 692 | + if (typeof value === "string" || typeof value === "number") { |
| 693 | + value = getDateFormatter().createDateFromValue(value); |
| 694 | + } |
| 695 | + let dateStyle = undefined, timeStyle = undefined; |
| 696 | + if (format === 'short' || format === 'medium' || format === 'long' || format === 'full') { |
| 697 | + dateStyle = format; |
| 698 | + timeStyle = format; |
| 699 | + } else if (format?.includes('Date')) { |
| 700 | + dateStyle = format.replace('Date', ''); |
| 701 | + } else if (format?.includes('Time')) { |
| 702 | + timeStyle = format.replace('Time', ''); |
| 703 | + } else if (format) { |
| 704 | + return getDateFormatter().formatDateCustomFormat(value, locale, format, false, timezone); |
| 705 | + } |
| 706 | + const options: Intl.DateTimeFormatOptions = { |
| 707 | + dateStyle, |
| 708 | + timeStyle, |
| 709 | + timeZone: timezone |
| 710 | + }; |
| 711 | + formattedDate = getDateFormatter().formatDateTime(value, locale, options); |
686 | 712 | } |
687 | | - if (typeof value === "string" || typeof value === "number") { |
688 | | - value = getDateFormatter().createDateFromValue(value); |
689 | | - } |
690 | | - let dateStyle = undefined, timeStyle = undefined; |
691 | | - if (format === 'short' || format === 'medium' || format === 'long' || format === 'full') { |
692 | | - dateStyle = format; |
693 | | - timeStyle = format; |
694 | | - } else if (format?.includes('Date')) { |
695 | | - dateStyle = format.replace('Date', ''); |
696 | | - } else if (format?.includes('Time')) { |
697 | | - timeStyle = format.replace('Time', ''); |
698 | | - } else if (format) { |
699 | | - return getDateFormatter().formatDateCustomFormat(value, locale, format, false, timezone); |
700 | | - } |
701 | | - const options: Intl.DateTimeFormatOptions = { |
702 | | - dateStyle, |
703 | | - timeStyle, |
704 | | - timeZone: timezone |
705 | | - }; |
706 | | - return getDateFormatter().formatDateTime(value, locale, options); |
| 713 | + |
| 714 | + return formattedDate; |
707 | 715 | } |
708 | 716 |
|
709 | 717 | function parseDigitsInfo(value?: string) { |
|
0 commit comments