15

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

POV
  • 11,293
  • 34
  • 107
  • 201

3 Answers3

25

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>
StepUp
  • 36,391
  • 15
  • 88
  • 148
7

Using angulars date pipe. Either:

HTML

{{ value_expression | date [ : format [ : timezone [ : locale ] ] ] }}

or Typescript

formatDate(value_expression, format, timezone, locale )

Sam
  • 1,736
  • 8
  • 15
1

Append 'UTC' to the string before converting it

var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
Seba Cherian
  • 1,755
  • 6
  • 18