最初,我对DatePipe进行了简单扩展,以转换标准(YYYYMMDD格式)中的日期值,以便Angular中的DatePipe可以使用它。
export class SlsDatePipe extends DatePipe implements PipeTransform {
constructor(Locale:string) {
super(Locale);
}
/**
* Convert the data from the numeric YYYYMMDD to the Javascript date format
* @param DateValue SLS Formatted number in the YYYYMMDD style
* @param args Angular Date Pipe args to control the output format
* @returns Text output following the Javascript date() formats
*/
public transform(DateValue:number, args:string):string | null {
const DateString:string = DateValue.toString();
const Year:number = parseInt(DateString.substr(0, 4), 10);
const Month:number = parseInt(DateString.substr(4, 2), 10) - 1;
const Day:number = parseInt(DateString.substr(6, 2), 10);
const DateObject:Date = new Date(Year, Month, Day);
return super.transform(DateObject, args);
}
该类中还进行了其他检查以确保格式正确,等等,但是问题的基本知识在那里。这在Angular 10中有效。我刚刚更新到Angular 11,现在代码在转换中产生错误:
error TS2416: Property 'transform' in type 'SlsTimePipe' is not assignable to the same property in base type 'DatePipe'.
Type '(TimeValue: number, args: string) => string' is not assignable to type '{ (value: string | number | Date, format?: string, timezone?: string, locale?: string): string; (value: null,
format?: string, timezone?: string, locale?: string): null; (value: string | number | Date, for mat?: string, timezone?: string, locale?: string): string; }'.
Type 'string' is not assignable to type 'null'.