Reputation: 1317
In my Angular
application, I have to display the date in 'MM/dd/yyyy
' format.
Web API is returning a date in "2020-12-01T00:00:00
" format.
When I use the below line of code, DatePipe
not recognizing the date but if I manually modify the date to "2020-12-01T00:00:00.000Z
" then DatePipe
converting to the expected 'MM/dd/yyyy
' format.
let res = this.datePipe.transform(new Date('2020-12-01T00:00:00'),'MM/dd/yyyy');
let res = this.datePipe.transform(new Date('2020-12-01T00:00:00.000Z'),'MM/dd/yyyy');
The first one is not working and the second one is working. But my API returning data in the first format.
Can someone help me to change the date from first to the second format using Typescript?
Upvotes: 0
Views: 2510
Reputation: 20494
new Date('...')
. Try console.log(isNaN(new Date('2020-12-01T00:00:00')))
. If the reuslt is true then you know the browser is the culprit.let res = this.datePipe.transform('2020-12-01T00:00:00','MM/dd/yyyy');
let res2 = this.datePipe.transform('2020-12-01T00:00:00.000Z','MM/dd/yyyy');
Upvotes: 1
Reputation: 409
Z means ISO 8601
try:
let res = this.datePipe.Transform(new Date('2020-12-01T00:00:00').toISOString(),'MM/dd/yyyy');
Upvotes: 1