Skip to content

Instantly share code, notes, and snippets.

@lopadz
Last active February 28, 2023 11:04
Show Gist options
  • Save lopadz/43ae80d5f8af72b4232233eaccb9df8e to your computer and use it in GitHub Desktop.
Save lopadz/43ae80d5f8af72b4232233eaccb9df8e to your computer and use it in GitHub Desktop.
Event Countdown Clock with Timezone Awareness

Event Countdown Clock with Timezone Awareness

jQuery.countdown is a great plugin to create countdowns, but it needs a bit of configuration to use it with event countdowns.

When you need a countdown clock that:

  • is aware of the event's Timezone
  • displays a notice when the event is in progress
  • displays a notice when the event has ended
  • hides elements when they reach zero
  • is instantiated using data-countdown attributes which lets you load multiple clocks on the same page ;)

Dependencies

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Event Countdown Clock with Timezone Awareness</title>
<!-- Normalize.css -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
<!-- Countdown Clock Styles -->
<link rel="stylesheet" href="styles.css">
<!-- jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- jQuery.countdown -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.countdown/2.2.0/jquery.countdown.min.js"></script>
<!-- MomentJS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>
</head>
<body>
<h1>Event Countdown Clock</h1>
<div class="countdown-clock">
<div class="countdown-container"
data-countdown
data-countdown-start="2020-01-01 12:00:00"
data-countdown-end="2020-01-01 12:00:00"
data-countdown-timezone="America/Chicago"></div>
</div>
<script>
// ID of the element to add the Countdown Clock to
var $clockElement = $('[data-countdown]');
$clockElement.each( function() {
// Read data from HTML attributes to store in vars
var $clock = $(this),
// Event STARTING time
eventStart = $(this).data('countdownStart'),
// Event ENDING time
eventEnd = $(this).data('countdownEnd'),
// Event TIMEZONE
eventTimeZone = $(this).data('countdownTimezone');
// CONVERT eventEnd to MOMENT.JS FORMAT to compare with current time later on.
var eventEndTime = moment.tz(eventEnd, eventTimeZone);
// Time to COUNTDOWN TO
var countdownTo = moment.tz(eventStart, eventTimeZone);
$clock.countdown(countdownTo.toDate(), { elapse: true } ).on('update.countdown', function(event) {
// Clock Countdown HTML Ouptut
var output = '<div class="time seconds"><span class="count">%S</span> <span class="label">sec%!S</span></div>';
// Add number of MINUTES left
if ( event.offset.totalMinutes > 0 ) {
output = '<div class="time minutes"><span class="count">%M</span> <span class="label">min%!M</span></div>' + output;
}
// Add number of HOURS left
if ( event.offset.totalHours > 0 ) {
output = '<div class="time hours"><span class="count">%H</span> <span class="label">hr%!H</span></div>' + output;
}
// Add number of DAYS left
if ( event.offset.totalDays > 0 ) {
output = '<div class="time days"><span class="count">%-D</span> <span class="label">day%!D</span></div>' + output;
}
// Get CURRENT TIME in the event's timezone
var currentTime = moment().tz(eventTimeZone);
// If CURRENT TIME is GREATER THAN event ENDING TIME, display...
if ( currentTime > eventEndTime && event.elapsed ) {
$(this).html(''
+ '<p style="color:red">Notice after the event has ENDED.</p>'
).parent().addClass('disabled');
// Stop the Countdown
$(this).countdown('stop');
// If event has ENDED and is still IN PROGRESS, display...
} else if ( event.elapsed ) {
$(this).html(''
+ '<p style="color:green">Notice while the event is IN PROGRESS.</p>'
).parent().addClass('disabled');
// Display COUNTDOWN to event...
} else {
$(this).html( event.strftime(output) );
}
});
});
</script>
</body>
</html>
/* --- Base --- */
html {
background-color: #304cc9;
font-size: 16px;
margin: 0;
padding: 0;
text-align: center;
}
body {
color: #fff;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-weight: normal;
line-height: 1.5;
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
/* Countdown Clock Styles */
.countdown-clock .countdown-container {
border-spacing: .5em;
border-collapse: separate;
display: table;
margin: .5em auto;
max-width: 500px;
width: 100%;
}
.countdown-clock .time {
background-color: #222;
border-radius: 0;
display: table-cell;
min-width: 50px;
padding: 1em .5em;
position: relative;
text-align: center;
}
.countdown-clock .count {
color: #fff;
display: block;
font-size: 2rem;
line-height: 1;
font-weight: bold;
width: 100%;
}
.countdown-clock .label {
color: #fff;
display: block;
font-size: 11px;
letter-spacing: 1px;
text-transform: uppercase;
width: 100%;
}
@media screen and (min-width: 768px) {
.countdown-clock .countdown-container {
border-spacing: 1em;
margin: 1em auto;
width: 100%;
}
.countdown-clock .time {
min-width: 100px;
padding: 1em;
}
.countdown-clock .count {
font-size: 2.5em;
line-height: 1.2;
}
.countdown-clock .label {
font-size: 15px;
}
}
/* --- Base --- */
html {
background-color: #304cc9;
font-size: 16px;
margin: 0;
padding: 0;
text-align: center;
}
body {
color: #fff;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;;
font-weight: normal;
line-height: 1.5;
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
/* Countdown Clock Styles */
.countdown-clock {
.countdown-container {
border-spacing: .5em;
border-collapse: separate;
display: table;
margin: .5em auto;
max-width: 500px;
width: 100%;
}
.time {
background-color: #222;
border-radius: 0;
display: table-cell;
min-width: 50px;
padding: 1em .5em;
position: relative;
text-align: center;
}
.count {
color: #fff;
display: block;
font-size: 2rem;
line-height: 1;
font-weight: bold;
width: 100%;
}
.label {
color: #fff;
display: block;
font-size: 11px;
letter-spacing: 1px;
text-transform: uppercase;
width: 100%;
}
@media screen and ( min-width: 768px ) {
.countdown-container {
border-spacing: 1em;
margin: 1em auto;
width: 100%;
}
.time {
min-width: 100px;
padding: 1em;
}
.count {
font-size: 2.5em;
line-height: 1.2;
}
.label {
font-size: 15px;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment