Creating an ics file is very similar to a csv file.  The main differences are in formatting, which are easily addressed.  In order to gather all of my data together and concatenate it appropriately, I created a function as with createcsv(), and called it createics().

The basic reason for this function is to create the necessary information for an ics calendar file, and return it so that it can easily be saved in ics format.  An ics file is very similar to the csv file, they just have different patterns in which the information is presented.  In an ics file you present a small bit of information and then ‘enter’ down to the next row.  A single calendar event in an ics file takes up 5 to 10 lines depending on how detailed the information is.  A csv calendar file will have the entire event take up one line, with commas separating the information.

function createics (schedule) {

 var finalics = "";

 var icsfilestart = "BEGIN:VCALENDAR" + "\n" + "VERSION:2.0" + "\n" + "PRODID:-//create ics file for use in mySam on Corvallis Sports Park website//EN" + "\n" + "CALSCALE:GREGORIAN" + "\n" + "X-WR-TIMEZONE;VALUE=TEXT:US/Pacific" + "\n";

 finalics += icsfilestart;

 for (var i = 0; i < schedule.length; i += 1){

   //adjusting time from dictionary 'schedule'

   //setting date

   var month = lessthanten(schedule[i].datetime.getMonth() + 1);

   var date = lessthanten(schedule[i].datetime.getDate());

   var year = schedule[i].datetime.getFullYear().toString();

   var basicdate = year + month + date;

   var dtstamp = "DTSTAMPT:" + year + month + date + "T" + "000000";

   ….. //setting time

    }

 finalics += "END:VCALENDAR";

 return finalics

};


To begin with I create an empty string called finalics.  This string will be the value returned from the function.  For all ics files, you have to start them out with the text outlines in the variable I call icefilestart.  This identifies that it is the beginning of a calendar event and provides other necessary information the file needs.  Then I add it to the finalics variable.  

After that I start my for loop.  This loop goes through all the information in schedule, going through each event on the CSP website just as the createcsv() function does.  As shown in my entry Issues and edits of code thus far, I’ve figured out ways to make my code much more compact.  This section show above used to be three times this size.  That made it difficult to read even if it was all simple code.  

Using my lessthanten() function, and by accessing schedule, using getMonth(), getDate(), getFullYear(), and toString() I’ve create my variables month, date, and year.  Then I concatenate them together to make my basicdate variable.  Then I integrate basicdate with the ics calendar formatting and create the dtstamp variable.  

Up next time: Creating an ics file, continued