Home > Software design >  Angular datepipe not working with BST timezone
Angular datepipe not working with BST timezone

Time:05-23

I am converting current date into British Summer Time (BST)

let date = new Date()
this.datePipe.transform(date, 'MMMM d, yyyy', 'BST');

It will return my current pc time after using datepipe transform also.

CodePudding user response:

I ran into that some time ago, you need to import the correct locale data.

Only the en-US locale data comes with Angular. To localize dates in another language, you must import the corresponding locale data. See the I18n guide for more information.

further details can be found in the Angular documentation on DatePipe, Angular documentation on Internationalisation and in depth discussions in another StackOverflow question (scroll down in the answers!)

CodePudding user response:

As mentioned in comments under your question, BST is not a valid timezone offset.

Internally, date pipe relies on the browser's handling of Date.parse (docs). All browsers should accept the standardized format containing time offset, e.g. 01 Jan 1970 00:00:00 GMT or 01 Jan 1970 00:00:00 GMT-02:00.

Some browsers will accept non-standard values, like the most common timezone abbreviations (e.g. PST, MST) for backward compatibility. Apparently BST is not one of those.

If you're willing to risk possible compatibility issues, you could try using relevant TZ database name in brackets instead of timezone offset, e.g. 01 Jan 1970 00:00:00 (Europe/London). This works on Chrome, and might work for other browsers, but again it's environment dependent as it's not part of the standard, so you would have to make sure it works on all platforms you support and can expect it to change over time.

  • Related