Skip to content

Instantly share code, notes, and snippets.

@thomas-kl1
Last active February 4, 2022 21:25
Show Gist options
  • Select an option

  • Save thomas-kl1/f1170901ad50a55782b3184acead545f to your computer and use it in GitHub Desktop.

Select an option

Save thomas-kl1/f1170901ad50a55782b3184acead545f to your computer and use it in GitHub Desktop.
Timezone aware countdown
// Print the date from the server side, in UTC+0
const endDate = new Date('2022-02-04T23:00:00+00:00');
const countdown = setInterval(function() {
const date = new Date();
const currentDate = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()));
const diff = endDate - currentDate;
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
console.log(days + "d " + hours + "h " + minutes + "m " + seconds + "s ");
if (diff < 0) {
clearInterval(countdown);
}
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment