Last active
February 4, 2022 21:25
-
-
Save thomas-kl1/f1170901ad50a55782b3184acead545f to your computer and use it in GitHub Desktop.
Timezone aware countdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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