I think this question has bean asked many time before. I have implemented DatePipe in my angular project, formating is correct only it shows the current Date and this is not what i want and is not the date that i receive from my rest services. I get from the REST service (createdOn) a date that is in the past. This is my code:
TS
import { DatePipe } from '@angular/common';
export class PostComponent implements OnInit {
post: Postpayload;
newDateFormat: string;
constructor(private router: ActivatedRoute,
private postService: PostService, public datepipe: DatePipe) {
this.post = {
createdOn: new Date()
};
this.newDateFormat = this.datepipe.transform(this.post.createdOn, 'dd-MM-yyyy');
}
}
PostPayload
export class Postpayload {
id: number;
content: string;
title: string;
username: string;
createdOn: Date;
}
ngOnInit(): void {
this.router.params.subscribe( params => {
this.permaLink = params.id;
});
this.postService.getPost(this.permaLink).subscribe( ( data:Postpayload)
=> {
this.post = data;
}, (err: any) => {
console.log(err);
console.log('failure response');
});
}
HTML
<p>{{ newDateFormat }}</p>
This results me a current date or todays date, my question is how do i format the date of this.post.createdOn that i receive from my rest Api service call. When i am not using DatePipe then it shows correct date but i dont want this default full style date format.
PostService
getPost(permaLink: number): Observable<Postpayload> {
return this.http.get<Postpayload>(this.url + 'get/' + permaLink);
}