Skip to content

Commit c3d9180

Browse files
committed
fix(localization): Fix formatDate not defaulting to angular's format date if available.
1 parent 834daea commit c3d9180

File tree

1 file changed

+30
-22
lines changed
  • projects/igniteui-angular/src/lib/core

1 file changed

+30
-22
lines changed

projects/igniteui-angular/src/lib/core/utils.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
formatDate as ngFormatDate,
23
getLocaleCurrencyCode,
34
getLocaleDateFormat as ngGetLocaleDateFormat,
45
getLocaleDateTimeFormat as ngGetLocaleDateTimeFormat,
@@ -681,29 +682,36 @@ export function getLocaleDateTimeFormat(locale: string, displayFormat?: string)
681682
* coalescing to an empty string.
682683
*/
683684
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);
686712
}
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;
707715
}
708716

709717
function parseDigitsInfo(value?: string) {

0 commit comments

Comments
 (0)