paperless-ngx/src-ui/src/app/pipes/custom-date.pipe.ts

34 lines
1 KiB
TypeScript
Raw Normal View History

import { DatePipe } from '@angular/common';
import { Inject, LOCALE_ID, Pipe, PipeTransform } from '@angular/core';
import { SettingsService, SETTINGS_KEYS } from '../services/settings.service';
2021-02-17 12:15:22 +01:00
const FORMAT_TO_ISO_FORMAT = {
"longDate": "y-MM-dd",
"mediumDate": "yy-MM-dd",
"shortDate": "yy-MM-dd"
}
@Pipe({
name: 'customDate'
})
export class CustomDatePipe extends DatePipe implements PipeTransform {
2021-02-25 16:22:50 +01:00
private defaultLocale: string
constructor(@Inject(LOCALE_ID) locale: string, private settings: SettingsService) {
2021-02-17 12:15:22 +01:00
super(locale)
2021-02-25 16:22:50 +01:00
this.defaultLocale = locale
}
transform(value: any, format?: string, timezone?: string, locale?: string): string | null {
2021-02-25 16:22:50 +01:00
let l = locale || this.settings.get(SETTINGS_KEYS.DATE_LOCALE) || this.defaultLocale
2021-02-17 12:15:22 +01:00
let f = format || this.settings.get(SETTINGS_KEYS.DATE_FORMAT)
if (l == "iso-8601") {
return super.transform(value, FORMAT_TO_ISO_FORMAT[f], timezone)
} else {
2021-02-25 16:22:50 +01:00
return super.transform(value, format || this.settings.get(SETTINGS_KEYS.DATE_FORMAT), timezone, l)
2021-02-17 12:15:22 +01:00
}
}
}