0 Replies Latest reply on Jul 17, 2014 2:58 PM by MatLac

    Why is number of days between dates not an integer?

    MatLac Level 1

      I wrote a script for constructing a calendar displaying the month chosen by the user.  It consist of a static 7x6 grid with fields numbered from 0 to 42.  What this script does basically is finding what day of the week is the first of the month and then laying out all dates, taking leap years in consideration and eveything.  No problem with this part.  This function returns cell1 which holds the number of the field (hence its name) that will display the first of the month.

       

      I need this variable in the second function for applying a color coded schedule overlay based on a team number.  Here is how it works:

      -Teams are numbered from 1 to 5

      -Schedule is a 35 days (5 weeks) rolling schedule

      -We have 3 possible shifts: night(blue), day(yellow), evening(red)

      -I used white for off-days

      The script works wonderful but only for 31 days months, all other months get offsetted by 31-x  (x = number of days in month).  I think it has something to do with the part where I calculate the number of complete 35 days cycle between a given "1st day of the month" and my reference day because I don't always get an integer.  I cheated it by flooring or ceiling the value.........

       

      function scheduleLayout(year, month, team, cell1)

      {

      //Defining the schedule order

      var r0 = color.white;

      var r1 = color.blue;

      var r2 = color.yellow;

      var r3 = color.red;

      var SCHEDULE = new Array(r0, r0, r1, r1, r1, r1, r1, r1, r1, r0, r0, r0, r0, r0, r0, r2, r2, r2, r2, r0, r0, r0, r3, r3, r0, r0, r2, r2, r2, r0, r0, r3, r3, r3, r0)

       

       

      //Calculating the starting index of the schedule for team 1

      var SCHEDULE_START_DATE = new Date(2013, 5, 19);

      var firstDay = new Date(year, month, 1);

      var diff = firstDay.getTime()-SCHEDULE_START_DATE.getTime();

      var oneDay = 24*60*60*1000;

      var team1StartingPoint = Math.round(diff/oneDay)%35;  //this might be the problem

       

       

      //offsetting weeks according to team

      var teamOffset = (team-1)*7;

      var startingPoint = team1StartingPoint+teamOffset;

      if (startingPoint > 34) startingPoint -= 35;

       

       

      //Coloring fields

      do{

        this.getField("header.date."+cell1).fillColor = SCHEDULE[startingPoint];

        startingPoint++;

        if (startingPoint > 34) startingPoint = 0;

        cell1++;

      } while (this.getField("header.date."+cell1).value >= 1);

      }