0

I am creating a countdown for an event and the server gives me the number of seconds left till this event. It works fine in the same time zone America/New_York but I am not sure how to achieve this for a different time zone. I guess I have to add/subtract a number of seconds based on user't time zone. I am taking into account that the number of seconds returned by the server will always be in EST. Can someone advise? So far I have this but I'm getting an error:

let serverZoneTime = new moment().tz.zone("America/New_York").offset(now); 
let currentZoneTime = moment.tz.guess().offset(now);

console.log((EstTzOffset - currentTz));
sayayin
  • 971
  • 5
  • 23
  • 36
  • If it's for a countdown then i don't think you need to consider timezone here as you have said "`server gives me the number of seconds left till this event` .If your server say "10 minutes remaining for shuttle launch" it will be as much true to someone in London or Tokyo as it's to you or the poor charlie on canary islands. Just make server return absolute ticks to an event and it will be good – Vinay Oct 18 '16 at 12:19
  • @Novice sorry you are absolutely right. Actually the counter is fine, I messed up my question. I need to show a message based on seconds remaining that event starts at 6 pm. So how do I add/subtract seconds from time in their timezone to always show 6pm? – sayayin Oct 18 '16 at 14:14
  • Yea got it you want to get the offset of users clock with respect to your own but you don't know his timezone looks like an easy problem if you could get his timezone but its not ! the problem is that due to DST, timezone offsets aren't constant throughout the year .I would suggest that don't go for manual calculation let moment.js do that have a look at [**this**](http://stackoverflow.com/questions/29265389/how-do-i-calculate-the-difference-of-2-time-zones-in-javascript) post it describes things in a great detail and also offers a simple but effective solution – Vinay Oct 18 '16 at 17:40
  • @novice Thanks for your help, I will follow this post, seems like what I need. – sayayin Oct 18 '16 at 20:17

1 Answers1

1

First of all, if this is an event at 6pm on a certain day I would get the exact timestamp or UTC time for that event start time. Below I'm using a fake timestamp.

This is important because the people viewing your event could change from EST to DST between "now" (which you are using above) and 6pm on the event day.

It sounds like you already have the countdown working but it is just the timezone issues you are dealing with so I'll skip the countdown logic.

const moment = require('moment-timezone');

// Get User's Timezone
const userTimezone = moment.tz.guess(); // this has to be on the client not server
const serverTimezone = "America/New_York";

// Get the exact timestamp of the event date apply the server timezone to it.
const serverEventTime = moment(34534534534, 'x').tz(serverTimezone);

// Take the server time and convert it to the users time
const userEventTime = serverEventTime.clone().tz(userTimezone);

// From here you can format the time however you want
const formattedUserEventTime = userEventTime.format('YYYY-MM-DD HH:mm:ss'); 
// Or format to a timestamp
const userEventTimestamp = userEventTime.format('x');

For a countdown you'll want the time now as well, which follows the same logic as above:

const serverTimeNow = moment().tz(serverTimezone);
const userTimeNow = serverTimeNow.clone().tz(userTimezone); 

// Get timestamp so we can do easier math with the time
const userNowTimestamp = userTimeNow.format('x');

Now all we have to do is subtract now time from the event time to get the difference and repeat every second using a setInterval() perhaps.

const millisecondsToEvent = userEventTimestamp - userNowtimestamp;
const secondsToEvent = millisecondsToEvent / 1000;

Hopefully that's useful to someone (just realized that this was two years old).

Joshua Dyck
  • 2,113
  • 20
  • 25