From b7201acece9a837235132fcdee8fd6f4e1a47fe4 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Mon, 18 Jan 2021 10:21:27 +0300 Subject: [PATCH] refactor format method of DateParserFormatter --- .../src/lib/utils/date-parser-formatter.ts | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/utils/date-parser-formatter.ts b/npm/ng-packs/packages/theme-shared/src/lib/utils/date-parser-formatter.ts index d0814e499c..cf8565435a 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/utils/date-parser-formatter.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/utils/date-parser-formatter.ts @@ -1,15 +1,8 @@ import { ApplicationLocalizationConfigurationDto, ConfigStateService } from '@abp/ng.core'; -import { DatePipe } from '@angular/common'; -import { Injectable, Optional } from '@angular/core'; +import { formatDate } from '@angular/common'; +import { Inject, Injectable, LOCALE_ID } from '@angular/core'; import { NgbDateParserFormatter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'; - -function padNumber(value: number) { - if (isNumber(value)) { - return `0${value}`.slice(-2); - } else { - return ''; - } -} +import snq from 'snq'; function isNumber(value: any): boolean { return !isNaN(toInteger(value)); @@ -21,7 +14,7 @@ function toInteger(value: any): number { @Injectable() export class DateParserFormatter extends NgbDateParserFormatter { - constructor(@Optional() private datePipe: DatePipe, private configState: ConfigStateService) { + constructor(private configState: ConfigStateService, @Inject(LOCALE_ID) private locale: string) { super(); } @@ -49,21 +42,15 @@ export class DateParserFormatter extends NgbDateParserFormatter { } format(date: NgbDateStruct): string { - const { shortDatePattern } = (this.configState.getOne( + if (!date) return ''; + + const localization: ApplicationLocalizationConfigurationDto = this.configState.getOne( 'localization', - ) as ApplicationLocalizationConfigurationDto).currentCulture.dateTimeFormat; + ); - if (date && this.datePipe) { - return this.datePipe.transform( - new Date(date.year, date.month - 1, date.day), - shortDatePattern, - ); - } else { - return date - ? `${date.year}-${isNumber(date.month) ? padNumber(date.month) : ''}-${ - isNumber(date.day) ? padNumber(date.day) : '' - }` - : ''; - } + const dateFormat = + snq(() => localization.currentCulture.dateTimeFormat.shortDatePattern) || 'yyyy-MM-dd'; + + return formatDate(new Date(date.year, date.month - 1, date.day), dateFormat, this.locale); } }