Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert datetime UTC to local time Angular? [duplicate]

I have UTC time in database.

I want to convert this time to local time in format dd.mm.yy h.m.s

How to do that?

My date is in UTC:

2019-08-08 08:57:59

In need local time +4:00

like image 348
OPV Avatar asked Aug 08 '19 09:08

OPV


1 Answers

Just append UTC before converting:

let yourDate = new Date('08/08/2019 12:22:48 PM UTC');
yourDate.toString();

OR:

let yourDate = new Date('08/08/2019 12:22:48 UTC');
yourDate.toString();

In addition you can use pipe which uses locale to display date in user's timezone. Try with client's timezone data::

<p>The date is {{today | date:'yyyy-MM-dd HH:mm:ss'}}</p>

Update:

For your case:

let dateString = '2019-08-08 08:57:59';
let yourDate: Date = new Date(dateString + ' UTC');

HTML:

<div>{{ today | date : 'EEEE, MMMM d, h:mm:ss' }} {{ today | date : 'a' | lowercase }}

if your time looks like '2019-08-08T08:57:59' then just concat 'z' at the end to state it as UTC then run through date pipe like

<div>{{ today+'Z' | date : 'EEEE, MMMM d, h:mm:ss' }}</div>
like image 98
StepUp Avatar answered Sep 25 '22 12:09

StepUp