Reputation: 155
i want to transform the date coming from the server in this format to this format ('yyyy-mm-dd hh-mm-ss')
Upvotes: 1
Views: 2400
Reputation: 36
<!--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
Upvotes: 2
Reputation: 4520
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
Upvotes: 1
Reputation: 41571
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
Upvotes: 0