I want to format the UTC datetime to 'dd/MM/yyyy hh:mm:ss a'. I tried this using the datepipe. However it converts it to the localtime. How can I format UTC to the required format.
Stackblitz link: https://stackblitz.com/edit/angular-oxr43w
Thanks in advance.
Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.
To change the timezone in Angular, pass the timezone offset ('0530′) or standard UTC/GMT (IST) or continental US timezone abbreviation, and it is an optional parameter. It represents the locale format rules to use. If set or undefined, the default value is our project locale (en_US).
Date pipe accepts a timezone check the docs
<p>
UTC: {{date | date:"dd/MM/yyyy hh:mm:ss a":"+0000"}}
</p>
Check this out
A simpler solution is to use the short parameter value set to "UTC"
:
<p>
UTC: {{date | date: 'short':'UTC'}}
</p>
Angular pipes cast dates to locale timezone unless you specify it: angular -datepipes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With