I have this code that does a calculation to find the a localized UTC time. Based on that UTC time there's another function that will calculate a next event date and time for me. If the time is between Sunday @ 12 and Wednesday @ 8 it will give me wednesdays event date, otherwise it will give me the next event date on Sunday. I then take that calculated date, split it apart and feed it in as parameter to a countdown timer.
I'm wondering if there's a more optimize way to do this, particularly with the conditional statement and where I split apart the time into parameters.
$(function () {
// Calculate the time locally
function calcLocalizedTime() {
var d = new Date(); // Date for current location
var utc = d.getTime() + (d.getTimezoneOffset() * 60000); // convert to msec add local time zone offset get UTC time in msec
var nd = new Date(utc + (3600000 * -5));
return nd; // return localized datetime
}
// Calculate the next Webcast DateTime
function calcNextWebCast(date) {
var localizedTime = new Date(date||new Date()); // localized datetime
var nextDate = new Date(date||new Date()); // Next webcast datetime object
var today = localizedTime.getDay() + '' + localizedTime.getHours(); // format localized datetime for comparison
if (today >= 012 && today <=320 ) { // is today between Sun @ 12 or Wednesday @ 8
nextDate.setDate(nextDate.getDate() + (3 - 1 - nextDate.getDay() + 7) % 7 + 1);
nextDate.setHours(20,0,0,0);
} else {
nextDate.setDate(nextDate.getDate() + (3 - 4 - nextDate.getDay() + 7) % 7 + 1);
nextDate.setHours(12,0,0,0);
}
return nextDate;
}
var localizedTime = calcLocalizedTime();
var nextWebCast = calcNextWebCast(localizedTime);
var m = nextWebCast.getMonth() + 1;
var d = nextWebCast.getDate();
var y = nextWebCast.getFullYear();
var hh = nextWebCast.getHours();
var mm = nextWebCast.getMinutes();
$('#defaultCountdown').countdown({until: $.countdown.UTCDate(-5, y, m-1, d, hh, mm)});
});