Angular convert UTC date to local date

Short and simple solution

ยท

1 min read

Recently I came across a situation where we needed to convert the given UTC datetime to the user's local datetime. The solution should be simple and expected to work for Daylight saving time (DST).

The date value is coming from API to Angular and the datetime should be shown across multiple places in the application.

Hence, I have done some research on Google and found below a simple elegant solution.

The date-time value 2023-10-25T12:00:57.703 is coming from API and missing time zone information.

Appending โ€œZโ€ to the given value indicates that the date is in UTC (Coordinated Universal Time). Also, this format matches the ISO 8601 specification.

2023-10-25T12:00:57.703Z //Added Z at the end of date time.

Now the Angular "date" pipe is successfully able to parse the UTC date and display the datetime in the user's local format.