If you need to do this string transformation in a component's template, the best choice (for performance reasons) is to use a pipe operator.
Use the solution proposed by xyz for the string transformation itself.
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: 'myDateFormat'
})
export class MyDateFormatPipe implements PipeTransform {
transform(value: string, args?: any): any {
if (!value) {
return value;
} else {
// process the 'value' param and return its result according to xyz's solution
}
}
}
then in your template, use it like this:
inputDate | myDateFormat
where inputDate is '19990813'.
And don't forget to import this class to the angular module that will use it.