Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format UTC using datepipe

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.

like image 990
Frenz Avatar asked Dec 26 '18 16:12

Frenz


People also ask

How do you display date time in UTC?

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.

How do you resolve diff timezone issue in angular?

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).


Video Answer


2 Answers

Date pipe accepts a timezone check the docs

<p>
  UTC: {{date | date:"dd/MM/yyyy hh:mm:ss a":"+0000"}}
</p>

Check this out

like image 112
Danil Gudz Avatar answered Oct 31 '22 09:10

Danil Gudz


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

like image 24
victorperezpiqueras Avatar answered Oct 31 '22 09:10

victorperezpiqueras