Browse Source

utc-to-local pipe added

pull/22619/head
erdemcaygor 1 year ago
parent
commit
8f2bb4838e
  1. 3
      npm/ng-packs/packages/core/src/lib/core.module.ts
  2. 1
      npm/ng-packs/packages/core/src/lib/pipes/index.ts
  3. 45
      npm/ng-packs/packages/core/src/lib/pipes/utc-to-local.pipe.ts

3
npm/ng-packs/packages/core/src/lib/core.module.ts

@ -30,6 +30,7 @@ import { ShortTimePipe } from './pipes/short-time.pipe';
import { ShortDatePipe } from './pipes/short-date.pipe'; import { ShortDatePipe } from './pipes/short-date.pipe';
import { SafeHtmlPipe } from './pipes/safe-html.pipe'; import { SafeHtmlPipe } from './pipes/safe-html.pipe';
import { provideAbpCoreChild, provideAbpCore, withOptions } from './providers'; import { provideAbpCoreChild, provideAbpCore, withOptions } from './providers';
import { UtcToLocalPipe } from './pipes';
const standaloneDirectives = [ const standaloneDirectives = [
AutofocusDirective, AutofocusDirective,
@ -64,6 +65,7 @@ const standaloneDirectives = [
ShortDateTimePipe, ShortDateTimePipe,
ShortTimePipe, ShortTimePipe,
ShortDatePipe, ShortDatePipe,
UtcToLocalPipe,
...standaloneDirectives, ...standaloneDirectives,
], ],
imports: [ imports: [
@ -85,6 +87,7 @@ const standaloneDirectives = [
ShortDateTimePipe, ShortDateTimePipe,
ShortTimePipe, ShortTimePipe,
ShortDatePipe, ShortDatePipe,
UtcToLocalPipe,
], ],
providers: [LocalizationPipe, provideHttpClient(withInterceptorsFromDi())], providers: [LocalizationPipe, provideHttpClient(withInterceptorsFromDi())],
}) })

1
npm/ng-packs/packages/core/src/lib/pipes/index.ts

@ -5,3 +5,4 @@ export * from './to-injector.pipe';
export * from './short-date.pipe'; export * from './short-date.pipe';
export * from './short-time.pipe'; export * from './short-time.pipe';
export * from './short-date-time.pipe'; export * from './short-date-time.pipe';
export * from './utc-to-local.pipe';

45
npm/ng-packs/packages/core/src/lib/pipes/utc-to-local.pipe.ts

@ -0,0 +1,45 @@
import { Pipe, PipeTransform, Injectable, inject } from '@angular/core';
import { TimezoneService } from '../services';
@Injectable()
@Pipe({
name: 'utcToLocal',
standalone: false,
})
export class UtcToLocalPipe implements PipeTransform {
protected readonly timezoneService = inject(TimezoneService);
private readonly timezone: string;
constructor() {
this.timezone = this.timezoneService.getTimezone();
}
transform(
value: string | Date | null | undefined,
locale: string = navigator.language,
options?: Intl.DateTimeFormatOptions,
): string {
if (!value) return '';
try {
const utcDate = new Date(value + 'Z'); // Ensure it's treated as UTC
const formatOptions: Intl.DateTimeFormatOptions = options || {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
};
const formatter = new Intl.DateTimeFormat(locale, {
...formatOptions,
timeZone: this.timezone,
});
return formatter.format(utcDate);
} catch (err) {
return '';
}
}
}
Loading…
Cancel
Save