devC
devC

Reputation: 1444

Angular Date pipe with Timezone doesn't convert properly

I'm passing a UTC datetime to the angular app and I want to display the time in a different timezone. This is what the returned date looks like on Postman:

"2021-07-21T09:15:00" - this is in UTC.

I want to convert this to a different timezone (determined at runtime) so I tried something like below, but it always show the same UTC value.

<ng-template kendoGridCellTemplate let-dataItem>
      {{ dataItem.returnedTime | date: 'hh:mm a' : 'IST' }}
    </ng-template>

I tried the following as well:

<ng-template kendoGridCellTemplate let-dataItem>
      {{ dataItem.returnedTime | date: 'shortTime' : 'IST' }}
    </ng-template>

 <ng-template kendoGridCellTemplate let-dataItem>
      {{ dataItem.returnedTime | date: 'hh:mm a' : '+530' }}
    </ng-template>

I expect this to show 2:45PM, instead it show 9:15AM.

What's wrong here?A

Upvotes: 3

Views: 5284

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241450

If that value is in UTC, it should have a Z on the end, as in "2021-07-21T09:15:00Z".

Per the ECMAScript spec:

... When the UTC offset representation is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.

Append a Z to the input string, either on your back end or when you receive it, and it should work fine.

Upvotes: 7

Related Questions