diff --git a/npm/ng-packs/packages/core/src/lib/core.module.ts b/npm/ng-packs/packages/core/src/lib/core.module.ts index e6e8c3355a..4238bcdb1e 100644 --- a/npm/ng-packs/packages/core/src/lib/core.module.ts +++ b/npm/ng-packs/packages/core/src/lib/core.module.ts @@ -65,7 +65,6 @@ const standaloneDirectives = [ ShortDateTimePipe, ShortTimePipe, ShortDatePipe, - UtcToLocalPipe, ...standaloneDirectives, ], imports: [ @@ -74,6 +73,7 @@ const standaloneDirectives = [ ReactiveFormsModule, RouterModule, LocalizationModule, + UtcToLocalPipe, ...standaloneDirectives, ], declarations: [ @@ -87,7 +87,6 @@ const standaloneDirectives = [ ShortDateTimePipe, ShortTimePipe, ShortDatePipe, - UtcToLocalPipe, ], providers: [LocalizationPipe, provideHttpClient(withInterceptorsFromDi())], }) diff --git a/npm/ng-packs/packages/core/src/lib/pipes/utc-to-local.pipe.ts b/npm/ng-packs/packages/core/src/lib/pipes/utc-to-local.pipe.ts index 6e5245fc6e..5667e1a4fc 100644 --- a/npm/ng-packs/packages/core/src/lib/pipes/utc-to-local.pipe.ts +++ b/npm/ng-packs/packages/core/src/lib/pipes/utc-to-local.pipe.ts @@ -1,28 +1,27 @@ import { Pipe, PipeTransform, Injectable, inject } from '@angular/core'; -import { TimezoneService } from '../services'; +import { LocalizationService, TimezoneService } from '../services'; @Injectable() @Pipe({ - name: 'utcToLocal', - standalone: false, + name: 'abpUtcToLocal', }) export class UtcToLocalPipe implements PipeTransform { protected readonly timezoneService = inject(TimezoneService); - private readonly timezone: string; - - constructor() { - this.timezone = this.timezoneService.getTimezone(); - } + protected readonly localizationService = inject(LocalizationService); transform( value: string | Date | null | undefined, - locale: string = navigator.language, options?: Intl.DateTimeFormatOptions, - ): string { + ): string | Date { if (!value) return ''; try { - const utcDate = new Date(value + 'Z'); // Ensure it's treated as UTC + const dateInput = new Date(value); + + if (isNaN(dateInput.getTime())) { + // Invalid date + return ''; + } const formatOptions: Intl.DateTimeFormatOptions = options || { year: 'numeric', @@ -32,14 +31,14 @@ export class UtcToLocalPipe implements PipeTransform { minute: '2-digit', }; - const formatter = new Intl.DateTimeFormat(locale, { + const formatter = new Intl.DateTimeFormat('en-US', { ...formatOptions, - timeZone: this.timezone, + timeZone: this.timezoneService.getTimezone(), }); - return formatter.format(utcDate); + return formatter.format(dateInput); } catch (err) { - return ''; + return value; } } }