Search code examples
angulardate-pipe

Angular 4 Date Pipe converting wrongly


I have rest service which returns a collection of objects and one of the field of the item is a date string (ISO-8601 format ) and the date value as follows

"createdDate" : "2017-02-21T12:56:50.907",

In the angular4 UI I put DatePipe to format the above date

{{resultItem.createdDate| date:'short'}}

and I am getting wrong conversion as follows

2/21/2017, 7:56 AM

instead of

2/21/2017, 0:56 AM


Solution

  • You may need to create a UTC date from your date with timezone... I assume you are in the pacific timezone as the time is 7 hours from UTC...

    Try this code to get a new date object without Timezone (assuming your variable is named "date"):

    var datewithouttimezone = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),  date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());