javascript – How to calculate the time difference, given the time zones?

Question:

I need to make a countdown timer to some event using the MomentJS library.

The server sends me the time (let's call it eventTime ), to which the countdown should go, I find out the difference between the current one and display it in HTML. Everything is simple here. The problem is that the server sends the time in Moscow time and the transformation eventTime = moment.utc(time) in the timer function does not give me anything.

How can I calculate time difference considering time zones?

Answer:

Ideally, of course, get time in UTC from the server. But if that's not possible, convert from Moscow time using moment timezone :

var t = moment.tz("2013-11-18 11:55", 'Europe/Moscow');

And don't forget to update the moment timezone library every time a new DST law comes into effect in the 'Europe/Moscow' zone (and the moment timezone guys will update the build).

An alternative option is to send from the server immediately the remaining time (for example, in seconds) until the desired event. In this case, it will not matter what time the client has (including if it is set incorrectly at all, as sometimes happens when trying to extend the trial versions of some programs).

Scroll to Top