0

I currently live in Europe and I work for an American client. Long story short, they have asked me to create a countdown banner for Mother's day coming up, and I am having a little bit of an issue making this countdown banner end time synchronous for the different timezones of the U.S.

const currentTime = new Date().getTime(); // Returning current time in miliseconds
const endDate = new Date('May 3, 2022 23:59:59').getTime(); // This returns the endDate in miliseconds

I'm not sure how to handle the endDate so that it returns the correct endDate whether the user is in Texas or El Paso or wherever. The endDate always needs to be May 3 of 2022

Thank you!

  • 1
    Re `new Date('May 3, 2022 23:59:59')`, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) For a more robust solution, use `new Date(2022, 4, 3, 23, 59, 59)`. – RobG Apr 27 '22 at 01:32

1 Answers1

0

Your timer should already be consistent across timezones as long as this JavaScript code is client-side. You constructed your endDate with a dateString without a time zone offset, so it is interpreted as the user's local time, and then stored as the number of milliseconds between January 1 1970 00:00:00 UTC and your endDate. The currentDate is also stored in the same way, so the time zone conversion is already handled. You can test this in Chrome DevTools with the Sensors tab by changing your location and verifying that the countdown timer is consistent, i.e. the difference in the countdown timer between two locations should be equal to the difference in their timezone offsets. For example, New York is 3 hours ahead of Los Angeles, so the countdown timer for New York should be -3 hours compared to the timer for Los Angeles.

gloo
  • 681
  • 2
  • 12