I need to do 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 output it in HTML. Everything is simple here. The problem is that the server sends time by MSK and the eventTime = moment.utc(time) conversion in the timer function gives me nothing.

How can I calculate the time difference, taking into account the time zones?

  • It is more correct to force the server to send time in utc or unix timestamp - Alexey Ten
  • And if the server is not forced? :) I actually had the idea to always take three hours from the coming time. But, it seems to me, it is a crutch. - Darina Goodwill
  • That's because three hours no one guarantees (who knows what our thoughts will come to mind) you need not crutches, do it in the normal way. And if you crutch, then take a moment tz and do not forget to update the timezone database - Alexey Ten
  • Kick backenders to send time from the server to UTC. - Vladimir Gamalyan
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Ideally, of course, getting time from the server in UTC. But if this is not possible, convert from Moscow time with the help of the moment timezone :

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

And do not forget to update the library of the moment timezone each time when the new law on summer time comes into force in the 'Europe / Moscow' zone (and the guys from the moment timezone update the assembly).

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