Search code examples
angularangular2-routingangular2-directivesangular2-pipe

How to transform date with angular 2?


i want to transform the date coming from the server in this format to this format ('yyyy-mm-dd hh-mm-ss')

enter image description here


Solution

  • <!--Import in app.module-->
    import { DatePipe } from '@angular/common';
    providers: [DatePipe]
    
    <!--HTML-->
    <input [ngModel]="startDate |date: 'yyyy-MM-dd'" name="startDate"/>
    <button type="button" (click)="transformDate(startDate)">Aceptar</button>
    
    <!--Component-->
    import { DatePipe } from '@angular/common';
    
    export class AppComponent {
       startDate: Date= new Date();
    
       constructor(private datepipe: DatePipe){}
    
      transformDate(date){        
             let transformdate: string;
             transformdate = this.datepipe.transform(date, 'MM/dd/yyyy');
             console.log(transformdate);
      }
    
    }
    html date= yyyy-MM-dd
    Component Transform date=: MM/dd/yyy