Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bug with object Date 1

Status
Not open for further replies.

majg2002

Programmer
Jul 11, 2000
8
0
0
ES
Look October
---------------

Award who fix the bug!!!!!!!!!!!!!!!!!!!!

This is the code. It's short, but difficult

Run it and change to October.


<html>

<head>
<title>Días de un mes</title>
<script>
var msPerDay = 24*60*60*1000; // Miliseconds 1 día (=86400000 ms);
function WriteDays()
{
yyyy=document.frm.year.options[document.frm.year.selectedIndex].value;
mm=document.frm.moth.options[document.frm.moth.selectedIndex].value;
var initialDate = new Date(yyyy,mm,1);
var contenido=&quot;&quot;;
var i=0;
while (mm==initialDate.getMonth())
{
i++;
contenido+= i + &quot;.&quot; + initialDate.toString() + &quot;<br>&quot;;
initialDate.setTime(initialDate.getTime() + msPerDay);
}
if (document.all)
{
document.all[&quot;capa&quot;].innerHTML=contenido;
}
else
{
document.layers[&quot;capa&quot;].document.open()
document.layers[&quot;capa&quot;].document.write(contenido)
document.layers[&quot;capa&quot;].document.close()
}
}
</script>
</head>

<body>
<form name=&quot;frm&quot;>
<table>
<tr>
<td>Year</td>
<td>
<select name=&quot;year&quot; onchange=&quot;WriteDays();&quot;>
<option value=&quot;2002&quot;>2002
<option value=&quot;2003&quot;>2003
<option value=&quot;2004&quot;>2004
<option value=&quot;2005&quot;>2005
<option value=&quot;2006&quot;>2006
<option value=&quot;2007&quot;>2007
</select>
</td>
</tr>
<tr>
<td>Month</td>
<td>
<select name=&quot;moth&quot; onchange=&quot;WriteDays();&quot;>
<option value=&quot;0&quot;>January
<option value=&quot;1&quot;>February
<option value=&quot;2&quot;>March
<option value=&quot;3&quot;>April
<option value=&quot;4&quot;>May
<option value=&quot;5&quot;>June
<option value=&quot;6&quot;>July
<option value=&quot;7&quot;>August
<option value=&quot;8&quot;>September
<option value=&quot;9&quot;>October
<option value=&quot;10&quot;>November
<option value=&quot;11&quot;>December
</select>
</td>
</tr>
</table>
</form>
<div id=&quot;capa&quot; style=&quot;position:absolute;left:225px; top:10px;height:10px; width:300px;&quot;>
</div>
</body>

</html>
 
How screwy! [shocked] You think that the js developers would have caught that one. Anyway, without knowing why you are using [tt]setTime()[/tt], here's one possible answer (changes to [tt]WriteDays()[/tt] in bold)

[tt]var i=1;
while (mm==initialDate.getMonth())
{
contenido+= i + &quot;.&quot; + initialDate.toString() + &quot;<br>&quot;;
i++;
initialDate.setDate(i);
}[/tt] Paul Ellis
[hammer] I've been programming since you had to punch holes in cards to code. (Always remember, number your cards!)
 

I already had several solutions. I didn't need a alternative algorithm, but to understand why the last Sunday of October is repeated when adding 86400000 ms (= 1 day) to the date with setTime() method.

It's due to 'Daylight Savings Time' of my Time Zone.

Really there isn't any bug, SORRY, the next example explains why the day is repeated:

Example
-------

........

(Fri Oct 25 00:00:00 UTC+0200 2002) + 86400000ms = Sat Oct 26 00:00:00 UTC+0200 2002. Day 26.

(Sat Oct 26 00:00:00 UTC+0200 2002) + 86400000ms = Sun Oct 27 00:00:00 UTC+0200 2002. Day 27.

(Sun Oct 27 00:00:00 UTC+0200 2002) + 86400000ms = Sun Oct 27 23:00:00 UTC+0100 2002. Day 27 (Daylight Savings Time applied and the day is repeated).

(Sun Oct 27 23:00:00 UTC+0100 2002)) + 86400000ms = Mon Oct 28 23:00:00 UTC+0100 2002. Day 28.

........





 
another attack of the day light savings bug I may not get it the 1st or 2nd time,
but how sweet that 15th time can be.
 
Why is it that the simplest solutions are always the last considered?

Does this effect happen when you &quot;spring forward&quot;, too? Paul Ellis
[hammer] I've been programming since you had to punch holes in cards to code. (Always remember, number your cards!)
 
Yes it does. I remember when April came around a lot of people where complaining about their scripts not working correctly. I may not get it the 1st or 2nd time,
but how sweet that 15th time can be.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top