Angular2 date pipe automatically adding timezone to date
Asked Answered
M

2

5

I have a date string, like this 1987-06-15T00:00:00.000Z, when I am added date pipe on it the date is showing a different date like Jun 14, 1987 in American time zone, but in India, it's showing correct.

<div>{{'1987-06-15T00:00:00.000Z' | date}}</div>
Machuca answered 9/8, 2017 at 5:31 Comment(2)
You want output like dd/MM/yyyy ?Prier
You can specify format in date pipe. If you are not happy with available date pipe formats, you can create and register your own pipe in the module.Soniasonic
M
4

This code is worked for me

<div>{{date_of_birth.split("T")[0] | date}}</div>
Machuca answered 9/8, 2017 at 6:4 Comment(1)
Cleaner/slower/better/worse? <div>{{date_of_birth | splice: 0:10 | date}}</div>Kinnikinnick
P
4

The string 1987-06-15T00:00:00.000Z represents different dates in different browser time zones.

Fix

If you don't want to change the date based on time zone, just use string parsing (e.g. substr) and not date parsing.

Polyunsaturated answered 9/8, 2017 at 5:35 Comment(3)
Is there is any option in date pipe?Machuca
Nope. It is meant to represent a given date in the user's time zone.Polyunsaturated
From the Source code: WARNINGS: - this pipe uses the Internationalization API. Therefore it is only reliable in Chrome and Opera browsers. github.com/angular/angular/blob/4.3.3/packages/common/src/pipes/…Unilateral
M
4

This code is worked for me

<div>{{date_of_birth.split("T")[0] | date}}</div>
Machuca answered 9/8, 2017 at 6:4 Comment(1)
Cleaner/slower/better/worse? <div>{{date_of_birth | splice: 0:10 | date}}</div>Kinnikinnick

© 2022 - 2024 — McMap. All rights reserved.