JavaScript Broadcast Calendar Calculation

Holy Crap! How long has it been since I posted anything here? Looks like a few months.Sorry about that. I happened to get kind of busy with a new and interesting project that consumed a lot of my time, and I didn't have anything useful I could share.

Well good news, I DO have something to share. Broadcast Calendars. In the world of TV, Radio, & Advertising, they decided that the calendar we all use was not good enough.So they had to make their own calendar for the planning and purchase of programs (new & syndication) and advertising.



Lets go over a how the rules of the calendar work.

  • Every week in the broadcast calendar starts on a Monday and ends on a Sunday
  • Every month has either four or five such weeks. Broadcast calendar months thus have either 28 or 35 days.
  • The first week of every broadcast month always contains the Gregorian calendar first of the month.
So thinking about this from an algorithm stand point, lets discuss how we can work with it. We only really need to find the lower bounds of each month.
Psudo code here.

Get First of Month.
Get Day of the week.
Subtract Days till Monday
Get first day of Broadcast Calendar.

Maybe it was the stress of working on other things, but this really baffled me for a bit. In an effort to add additional functionality to a report that was being generated, I needed to calculate where days were in the proper broadcast month. The true difference in days wasn't really a requirement so I was able to come up with this as the following solution. I hope this is helpful to some one!

/*Function: BroadcastCalculator * -------------------- * Recives a date in the format of mm-dd-yyyy, and returns a date in the broadcast month * that the date would be in. This is to preserve other date calculations based on number * of months * * returns: Date in mm-dd-yyyy format, in the broadcast month the date resides. */ function BroadcastCalculator(Input){ //Declare varables used in all areas var OutPut = Input; //First Format the date to a date format useable by JavaScript var ParseTheDateToArray = Input.split("-"); var InputDate = new Date (ParseTheDateToArray[0]+"/"+ParseTheDateToArray[1]+"/"+ParseTheDateToArray[2]); //If Date CAN be a Broadcast Date if (ParseTheDateToArray[1] >= 23){ ///////// ////NOTE: 23 is used as an absolute edge case. The earliest a day could ever be in the next broadcast month. ///////// If March 1st is on a Sunday, and a non leap year, Monday Feb 23 would be in March. var month; var day = 1; var year=ParseTheDateToArray[2]; var BroadcastDate; var DayOfTheWeek; //Calculate the Broadcast Month Starting Day. //Check to see if December if(ParseTheDateToArray[0] == 12){ //Is December month = 1; year=parseInt(ParseTheDateToArray[2])+1; } else{ //Is NOT December month = parseInt(ParseTheDateToArray[0])+1; }//End December Check BroadcastDate = new Date (month+"/"+day+"/"+year); DayOfTheWeek = BroadcastDate.getDay(); //if the DayOfTheWeek is sunday, set to 7. This is just to make the math easier. Sunday is 0. if(DayOfTheWeek == 0){ DayOfTheWeek = 7; } //Determine the Broadcast Month start day. Loops till Monday while (DayOfTheWeek > 1 ){ BroadcastDate.setDate (BroadcastDate.getDate() - 1); DayOfTheWeek--; } //We Now have the First Day of the Broadcast Month Set! //Compare the Dates. if (InputDate >= BroadcastDate){ //Input is after Broadcast. Broadcast becomes the new date. OutPut = month+"-"+day+"-"+year; }//Else Output=Input. End Date Compare }//End if Date Can be BroadCast //In all other outcomes. Input is Output. return OutPut; }

Comments

Popular posts from this blog

PowerShell PGP Decryption

Powershell to Create Custom theme for Modern SharePoint Pages