Browse Source

Merge pull request #7294 from abpframework/fix/7290

Refactored format method of DateParserFormatter
pull/7295/head
Mehmet Erim 6 years ago
committed by GitHub
parent
commit
f420b56d23
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 37
      npm/ng-packs/packages/theme-shared/src/lib/utils/date-parser-formatter.ts

37
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 { ApplicationLocalizationConfigurationDto, ConfigStateService } from '@abp/ng.core';
import { DatePipe } from '@angular/common'; import { formatDate } from '@angular/common';
import { Injectable, Optional } from '@angular/core'; import { Inject, Injectable, LOCALE_ID } from '@angular/core';
import { NgbDateParserFormatter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'; import { NgbDateParserFormatter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
import snq from 'snq';
function padNumber(value: number) {
if (isNumber(value)) {
return `0${value}`.slice(-2);
} else {
return '';
}
}
function isNumber(value: any): boolean { function isNumber(value: any): boolean {
return !isNaN(toInteger(value)); return !isNaN(toInteger(value));
@ -21,7 +14,7 @@ function toInteger(value: any): number {
@Injectable() @Injectable()
export class DateParserFormatter extends NgbDateParserFormatter { export class DateParserFormatter extends NgbDateParserFormatter {
constructor(@Optional() private datePipe: DatePipe, private configState: ConfigStateService) { constructor(private configState: ConfigStateService, @Inject(LOCALE_ID) private locale: string) {
super(); super();
} }
@ -49,21 +42,15 @@ export class DateParserFormatter extends NgbDateParserFormatter {
} }
format(date: NgbDateStruct): string { format(date: NgbDateStruct): string {
const { shortDatePattern } = (this.configState.getOne( if (!date) return '';
const localization: ApplicationLocalizationConfigurationDto = this.configState.getOne(
'localization', 'localization',
) as ApplicationLocalizationConfigurationDto).currentCulture.dateTimeFormat; );
if (date && this.datePipe) { const dateFormat =
return this.datePipe.transform( snq(() => localization.currentCulture.dateTimeFormat.shortDatePattern) || 'yyyy-MM-dd';
new Date(date.year, date.month - 1, date.day),
shortDatePattern, return formatDate(new Date(date.year, date.month - 1, date.day), dateFormat, this.locale);
);
} else {
return date
? `${date.year}-${isNumber(date.month) ? padNumber(date.month) : ''}-${
isNumber(date.day) ? padNumber(date.day) : ''
}`
: '';
}
} }
} }

Loading…
Cancel
Save