[clug-progsig] Daylight savings breaking my code

Shawn sgrover at open2space.com
Wed Oct 25 14:36:15 PDT 2006


As a hack I tried adding 0.5 to x where the cd variable is set.  This 
seems to resolve the problem.  But, it IS a hack.  I would still prefer 
something a little more "proper"...

Shawn

Shawn wrote:
> I have an odd problem with one of my JS routines.  The Javascript part 
> itself is not the problem, it's the logic - I think...
> 
> Somehow the following code generates two Oct 29ths - which is when we 
> need to reset our clocks.  The code that is causing this is this loop:
> 
> for (var x=0;
>       x < Math.abs(datediff('d', this.startdate, this.enddate)) + 1;
>       x++) {
> 
>    var cd = dateadd('d', x, this.startdate);
>    var dtr = document.createElement("td");
>    dtr.appendChild(document.createTextNode(cd.format('dd-NNN')));
>    dtr.className = classHeader;
>    htr.appendChild(dtr);
> }
> 
> As you can see, all we are doing is counting from 0 to however many days 
> are in the startdate/enddate range.  (i.e. From Oct 21st to Oct 31st 
> would be 11 days - including the 21st).  Then we use the current count 
> to indicate how many days to add to the start date, to arrive at a 
> current date for a column.  The last four lines are doing nothing but 
> creating a table cell with that date as the text.
> 
> I've dumped the value of x and cd (right after CD is set), and get the 
> following:
> 
> x: 0 -- cd: Sun Oct 22 00:00:00 MDT 2006
> x: 1 -- cd: Mon Oct 23 00:00:00 MDT 2006
> x: 2 -- cd: Tue Oct 24 00:00:00 MDT 2006
> x: 3 -- cd: Wed Oct 25 00:00:00 MDT 2006
> x: 4 -- cd: Thu Oct 26 00:00:00 MDT 2006
> x: 5 -- cd: Fri Oct 27 00:00:00 MDT 2006
> x: 6 -- cd: Sat Oct 28 00:00:00 MDT 2006
> x: 7 -- cd: Sun Oct 29 00:00:00 MDT 2006
> x: 8 -- cd: Sun Oct 29 23:00:00 MST 2006
> x: 9 -- cd: Mon Oct 30 23:00:00 MST 2006
> x: 10 -- cd: Tue Oct 31 23:00:00 MST 2006
> 
> Notice that when x==7, the timezone is MDT, but from 8 on it is MST.
> 
> The dateadd() function is pretty basic:
> 
> function dateadd(timeU,byMany,dateObj) {
>   var millisecond=1;
>   var second=millisecond*1000;
>   var minute=second*60;
>   var hour=minute*60;
>   var day=hour*24;
>   var week=day*7;
>   var year=day*365;
> 
>   var newDate;
>   var dVal=dateObj.valueOf();
>   switch(timeU) {
>      case "ms": newDate=new Date(dVal+millisecond*byMany); break;
>      case "s": newDate=new Date(dVal+second*byMany); break;
>      case "m": newDate=new Date(dVal+minute*byMany); break;
>      case "h": newDate=new Date(dVal+hour*byMany); break;
>      case "d": newDate=new Date(dVal+day*byMany); break;
>      case "w": newDate=new Date(dVal+week*byMany); break;
>      case "y": newDate=new Date(dVal+year*byMany); break;
>   }
> }
> 
> So the routine does not seem to be accounting for daylight savings time 
> properly...  Any suggestions on how I can resolve this?
> 
> Shawn
> 
> 
> 
> _______________________________________________
> clug-progsig mailing list
> clug-progsig at clug.ca
> http://clug.ca/mailman/listinfo/clug-progsig_clug.ca
> 
> 





More information about the clug-progsig mailing list