Skip to content

Instantly share code, notes, and snippets.

@stevenschobert
Last active December 13, 2015 19:28
Show Gist options
  • Select an option

  • Save stevenschobert/4962986 to your computer and use it in GitHub Desktop.

Select an option

Save stevenschobert/4962986 to your computer and use it in GitHub Desktop.
Run a podcast? Here's a little JavaScript function to test if your show is currently "On the Air".
/**
* onAir.js
*
* @param {string} day [the day your show airs (eg. "Tuesday")]
* @param {string} start [start time of the show in 24hr format (eg. "13:00")]
* @param {string} end [stop time of the show in 24hr format (eg. "14:30")]
* @param {string} timezone [the UTC time zone of your show (eg. "-6" for Central Time)]
* @return {bool}
*/
var onAir = function (day, start, end, timezone) {
var local, utc, show, days, onAir, startValues, endValues, startTime, endTime,
startMinutes, endMinutes, showMinutes;
// by default, we are not on air
onAir = false;
// map day numbers to indexes
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Firday', 'Saturday'];
// convert start/end times to date objects
startValues = start.split(':');
endValues = end.split(':');
startTime = new Date();
endTime = new Date();
startTime.setHours(startValues[0], startValues[1]);
endTime.setHours(endValues[0], endValues[1]);
// add the hours minutes together to get total minutes
startMinutes = (startTime.getHours() * 60) + startTime.getMinutes();
endMinutes = (endTime.getHours() * 60) + endTime.getMinutes();
// get the current local time
local = new Date();
// get the current time in the show's timezone
utc = local.getTime() + (local.getTimezoneOffset() * 60000);
show = new Date(utc + (3600000*timezone));
// convert the show hours + minutes to just minutes
showMinutes = (show.getHours() * 60) + show.getMinutes();
// test to see if the show is going on right now
if (days[show.getDay()] === day && (showMinutes >= startMinutes && showMinutes <= endMinutes)) {
onAir = true;
}
return onAir;
}

onAir.js

Hey podcasters! I wrote this little function for a SO question and thought it might be useful to someone else.

It's a quick true/false test to see if your show is on the air!

Example

Let's say I host a show from 2:00-2:30pm Central Time (-6 UTC). Here's how to test that using onAir.js

var isShowing = onAir('Tuesday', '14:00', '14:30', '-6');

Now isShowing will be either true or false if our show is currently airing!

Add some jQuery

A practice use case for this function would be show or hide a <div> on your page if your show is on the air:

$(document).ready(function () {
  // test if our show is live
  var isShowing = onAir('Tuesday', '14:00', '14:30', '-6');

  // show our div if we are!
  if (isShowing) {
    $('#wearelive').show();
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment