1

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

o.O
  • 155
  • 5
  • 16

3 Answers3

2
<!--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
1

You could create your own pipe :

@Pipe({name: 'myDate'})
export class MyDatePipe implements PipeTransform {
  transform(date: Object): string {
    return `${date.year}-${date.monthOfYear}-${date.dateOfMonth} ${date.hourOfDay}-${date.minuteOfHour}-${date.secondOfMinute}`;
  }
}

And use it like this : <div>{{ yourDateToFormat | myDate }}</div>

Check out the doc, it's pretty clear : https://angular.io/docs/ts/latest/guide/pipes.html

soywod
  • 4,377
  • 3
  • 26
  • 47
0

You can directly append the string which is the easiest solution for your data

var string =  date.year + '-' + date.monthOfYear + '-' + date.dateOfMonth + ' ' + 'date.hourOfDay'+ '-' + date.minuteOfHour + '-' + date.secondOfMinute
Aravind
  • 40,391
  • 16
  • 91
  • 110