Date format change angular 2

后端 未结 3 818
耶瑟儿~
耶瑟儿~ 2020-12-19 13:31

I need to change my date format, the problem is that I can\'t use moment.js, I need just to transform date from yyyy-mm-dd to dd-mm-yyyy. I use angular v 2.4.0.

3条回答
  •  死守一世寂寞
    2020-12-19 13:59

    You can do it creating custom Pipe. Refer below code. For more info refer DatePipe Documentation.

    import { Pipe, PipeTransform } from '@angular/core';
    import { DatePipe } from '@angular/common';
    
    @Pipe({
        name: 'customDateFormat',
    })
    export class customDateFormatPipe implements PipeTransform {
        transform(value: string) {
           var datePipe = new DatePipe("en-US");
            value = datePipe.transform(value, 'dd-mm-yyyy');
            return value;
        }
    }
    

    Add custom pipe in html as shown below:

    {{currentDate | customDateFormat }}
    

提交回复
热议问题