Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moment js is picking up the wrong timezone/offset, so the conversion from UTC to local time is incorrect

I have read several articles, the docs at momentjs and several issues (here on stackoverflow) regarding moment and timezone handling, but I have not found the answer to how moment decides what the local UTC offset is. I am in the America/Denver timezone and at this time of this writing, the offset is UTC-07:00. The Windows clock is set correctly and displays the local time properly. For a UTC timestamp of "2017-04-24 17:33:55", I expect the following code to log displayDate as "04/24 10:12:34"

dateString = "2017-04-24 17:12:34"
console.log("dateString: " + dateString)
utc = moment.utc(dateString)
console.log("utc: " + utc.format())
now = moment(utc.format())
console.log("now: " + now.format())
displayDate = now.format("MM/DD HH:mm:ss")
console.log("displayDate: " + displayDate)

But, this is what the console shows:

dateString: 2017-04-24 17:12:34
utc: 2017-04-24T17:12:34Z
now: 2017-04-24T18:12:34+01:00
displayDate: 04/24 18:12:34

I have no idea how it thinks I'm in a timezone with offset of UTC+01:00. I am not using moment-timezone and have been unable to resolve why moment is picking up the wrong offset. I noticed there is a TZ environment variable set to America/Denver, so this clearly has no effect. How does moment determine the local offset? Is there some sort of default setting that is messed up somewhere? It must be something on my machine, because the same code works properly on another machine.

In case it helps, this code lives in an AngularJS (version 1.5.8) app.

Cheers, Rich

like image 303
Rich R Avatar asked Apr 25 '17 00:04

Rich R


People also ask

Does moment convert UTC to local?

To convert UTC time to Local you have to use moment. local() .

How do I get local time from UTC and offset?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11.

Does moment js use local timezone?

By default, moment objects are created in the local time zone. Local time zone - it's a time zone which is set in a browser or on your node.js server. To change the default time zone, use moment.tz.setDefault with a valid time zone.


2 Answers

From the normal JavaScript Date().toString() output, it gave me a clue about the TZ environment variable as it was set to "America/Denver". I unset the TZ environment variable, then restarted the web server (Tomcat service) and tried again. I got the same problem, but decided to reboot the whole machine anyway. After all, that's the solution to lots of issues on Windows machines... Sure enough, that worked!

The bottom line is that JavaScript must be using the TZ environment variable (overriding the system date & time settings) and it seems to be only looking at the first three characters.

Hope this helps someone else.

like image 191
Rich R Avatar answered Jan 03 '23 14:01

Rich R


If you are using moment.js then you can convert UTC to local timezone.

First you will add moment.js library

Then after that define a variable date and assign UTC time zone date time value like

var date = '2017-12-25 10:50:18';

//(UTC datetime)

var stillUtc = moment.utc(date).toDate();

//Converting in local time zone

var local = moment(stillUtc).local().format('YYYY-MM-DD HH:mm:ss');

Get local date time in local variable. You can print local variable and check your answer

console.log(local);

Your console result is '2017-12-25 16:20:18'

like image 35
Raman Singh Avatar answered Jan 03 '23 12:01

Raman Singh