Search code examples
angulardatetimeangular-pipedate-pipeangular-date-format

InvalidPipeArgument for DatePipe, IST timezone not supported by default


I have tried using DatePipe to format date values which I get from REST API. The format of the date string in REST API is like this: Wed Mar 28 12:20:11 IST 2018.

It works fine when the timezone in the string is UTC, EST etc. However it is resulting in below error if the timezone string is IST, CET or some other.

ERROR Error: InvalidPipeArgument: 'Wed Mar 28 12:20:11 IST 2018' for pipe 'DatePipe'
at invalidPipeArgumentError (VM616 common.umd.js:4313)
at DatePipe.transform (VM616 common.umd.js:4495)
at MyPipe.transform (VM1283 my.pipe.ts!transpiled:20)

A sample reproducer is available here: https://plnkr.co/edit/UPNJG9ejpYdkYQivq3cj?p=preview. I tried creating a DatePipe instance as below, but it did not work.

new DatePipe('en-IN')

Could someone please tell me how to add support for timezones which are not supported by default in DatePipe.

Thanks in advance.


Solution

  • After going through the documentation for DatePipe in more detail, I stumbled upon this..

    timezone to be used for formatting. It understands UTC/GMT and the continental US time zone abbreviations, but for general use, use a time zone offset, for example, '+0430' (4 hours, 30 minutes east of the Greenwich meridian) If not specified, the local system timezone of the end-user's browser will be used.

    Sending a format with GMT+530 or just +530 instead of IST worked fine. Finally I ended up modifying my format like Wed Mar 28 12:20:11 GMT+530 2018