Question

I was writing a chrome extension this morning and the everything seems fine.

However by night time I had noticed that the time being displayed is wrong. At first I thought it was a code problem but after going onto their site it seems like every single places the time were all displaying my local time.

Is anyone experiencing this same problem? Or should I consider changing the library all together...

I've recorded a video here http://youtu.be/g6n6nXJt_uU

function setClock() { now = moment.tz(moment.tz().format("MM/DD/YYYY hh:mm"), "EST").format("MM/DD/YYYY hh:mma"); document.getElementById("time").innerHTML=now + " NYC"; } console.log(moment.tz("EST")._d); console.log(moment()._d); setClock(); setInterval(setClock,1000);

CodeOutputHere

Was it helpful?

Solution

Regarding your code:

  • You should not use time zone abbreviations like EST for this purpose, since time zone abbreviations are often ambiguous. If you're looking to set US Eastern Time, then you should be using the IANA time zone identifier of America/New_York.

    EST only exists in the tzdb for backwards compatibility purposes. Arguably, it should be left out of the moment-timezone data.

  • Calling moment.tz() without any argument is not recommended. You should just call moment().

  • Your code appears to be formatting a moment, then parsing it into another moment, then formatting it out again. There's no reason to jump through so many hoops.

  • You should not use the _d property. That is meant to be internal to moment.js. (It should probably be hidden by closure, but it's not). Instead, use .format() or one of the other public functions.

Ultimately, I think you are simply after:

var now = moment().tz('America/New_York').format("MM/DD/YYYY hh:mm");

I highly suggest you re-read the moment-timezone and moment.js documentation.

Regarding the moment-timezone web site with the map - you're correct that there's currently a bug with the demonstration web site. It is not a bug with the library itself. I'll investigate and report that to the moment team.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top