TechnoCorner
TechnoCorner

Reputation: 5135

Converting server time to localtime momentjs

I have an API which gives me the following info

Backend API:

/api/time

{
  "timeZoneOffset": -18000000, (milli sec)
  "serverTimeUTC": 1485332569157,
  "serverTime": "Wed Jan 25 03:22:49 EST 2017",
  "timeZone": "Eastern Standard Time"
}

I have another API through which I get time from reports

API: /reports/

{
"reportTime": "01/24/2017 12:06 AM"
}

How can I display the reportTime in local system time? (browser time?)

I've tried:

browserTime = moment(reportTime).utcOffset(moment().utcOffset()).format('MM/DD/YYYY h:mm A');

(I'm currently in PST so offset is -480)

I'm not able to get this working. Please help.

Upvotes: 1

Views: 428

Answers (1)

You can convert reportTime to local time by doing:

var localTime  = moment.utc(reportTime).toDate();
    localTime = moment(localTime).format('YYYY-MM-DD HH:mm:ss');

More convinient way use http://momentjs.com/timezone/. It enables conversion from EST to PST

Upvotes: 1

Related Questions