PDA

View Full Version : Basic Calendar Obvious Bug



Den7
12-06-2009, 02:08 PM
1) Script Title: Basic Calendar

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex7/basiccalendar.htm

3) Describe problem: hehe, it is cearly shown on the URL above. :D Go to the section where there are 3 months displayed in a row. It stops executing on December 2009. It also clearly says, "undefined - 2009" instead of January 2010. :eek: Does anybody know how to fix it? I run it on my web-site too and it does not look cool any more. :-(

bluewalrus
12-06-2009, 02:37 PM
I think it's cause the month is just being added with no consideration for what the value becomes. Try putting in a conditional statement in. This is example code not functioning code (i think).



<script type="text/javascript">
var todaydate=new Date()
var curmonth=todaydate.getMonth()+1 //get current month (1-12)
var curyear=todaydate.getFullYear() //get current year
</script>
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="33%">
<script>
document.write(buildCal(curmonth-1 ,curyear, "main", "month", "daysofweek", "days", 1));
</script></td>
<td width="33%">
<script>
document.write(buildCal(curmonth ,curyear, "main", "month", "daysofweek", "days", 1));
</script></td>
<td width="34%">
<script>
if (curmonth +1 > 12) {
curmonth = curmonth - 12;
curyear = curyear + 1;
}
document.write(curmonth+1 ,curyear, "main", "month", "daysofweek", "days", 1);
</script></td>
</tr>
</table>

Den7
12-06-2009, 02:44 PM
... I mean I know how to fix it manually but are there any constant fixes in code so that when it comes to the end of the year I would not have to do it manually?

Until the year jumps to the first 3 months January February March or any other 3 months in a row within the same year it's OK. When it changes years for example November 2009, December 2009, and January 2010 (or any other versions finishing one year and entering into the next year) we run into a bug as January 2010 is not shown.

bluewalrus
12-06-2009, 03:10 PM
That code says

If the current month + 1 is greater then 12, subtract 12 from the current month and add 1, increase the year by 1.

The only instance this will occur is December subtracting 12 brings it back to 0 and adding 1 to it sets it to 1, or January. The current year is then increased by 1 as well to account for the 12 months that were subtracted. This will also need to be done but in reverse order for January, I think. This is a fix ( if it works), it doesn't require a rework every year the year is being called dynamically, it's not static.

Den7
12-06-2009, 03:13 PM
************************
Thanks for a great tip! Conditional statements work! Why didn't I think of it myself too? lol Now we also need to be added into this copy right's stuff to be the bug finders and co-authors of the script hehe. :rolleyes: Anyway, I suggest they fix it on dynamicdrive.com as well 'cause displaying the code together with this bug included looks a little wierd :cool: :D
************************


I think it's cause the month is just being added with no consideration for what the value becomes. Try putting in a conditional statement in. This is example code not functioning code (i think).

<script type="text/javascript">
var todaydate=new Date()
var curmonth=todaydate.getMonth()+1 //get current month (1-12)
var curyear=todaydate.getFullYear() //get current year
</script>
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="33%">
<script>
document.write(buildCal(curmonth-1 ,curyear, "main", "month", "daysofweek", "days", 1));
</script></td>
<td width="33%">
<script>
document.write(buildCal(curmonth ,curyear, "main", "month", "daysofweek", "days", 1));
</script></td>
<td width="34%">
<script>
if (curmonth +1 > 12) {
curmonth = curmonth - 12;
curyear = curyear + 1;
}
document.write(curmonth+1 ,curyear, "main", "month", "daysofweek", "days", 1);
</script></td>
</tr>
</table>

Den7
12-06-2009, 03:23 PM
I hear you and understand your code :) Thanks again. You were simply quicker in replying. I thought I would add another explanation to my first post but you got in with your response faster :D


That code says

If the current month + 1 is greater then 12, subtract 12 from the current month and add 1, increase the year by 1.

The only instance this will occur is December subtracting 12 brings it back to 0 and adding 1 to it sets it to 1, or January. The current year is then increased by 1 as well to account for the 12 months that were subtracted. This will also need to be done but in reverse order for January, I think. This is a fix ( if it works), it doesn't require a rework every year the year is being called dynamically, it's not static.

bluewalrus
12-06-2009, 03:31 PM
Well the code itself works just the demo is incorrect. This one has the workaround for January, assuming the other worked. To test it adjust the calender on your computer.


<script type="text/javascript">
var todaydate=new Date()
var curmonth=todaydate.getMonth()+1 //get current month (1-12)
var curyear=todaydate.getFullYear() //get current year
</script>
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="33%">
<script>
if (curmonth - 1 < 1) {
curmonth = curmonth + 12;
curyear = curyear - 1;
}
document.write(buildCal(curmonth-1 ,curyear, "main", "month", "daysofweek", "days", 1));
</script></td>
<td width="33%">
<script>
document.write(buildCal(curmonth ,curyear, "main", "month", "daysofweek", "days", 1));
</script></td>
<td width="34%">
<script>
if (curmonth +1 > 12) {
curmonth = curmonth - 12;
curyear = curyear + 1;
}
document.write(curmonth+1 ,curyear, "main", "month", "daysofweek", "days", 1);
</script></td>
</tr>
</table>

Den7
01-20-2010, 09:17 AM
hey, now the middle part has a problem. The last month of the previous year (December 2009 for example) shows OK, January 2010 (in the middle) is indefined and February 2010 (the last one) is OK as well. Which conditional do I add for this case? Thanks.

Den7
01-20-2010, 09:23 AM
on the second thought the third's case conditional suits perfectly well but are there any "under water hidden" bugs? Will it not stop working somewhere down the road will it? Any ideas anybody?

if (curmonth +1 > 12) {
curmonth = curmonth - 12;
curyear = curyear + 1;
}

Den7
12-14-2010, 08:22 AM
hey, now the middle part has a problem. The last but one month of the previous year (November 2010 for example) shows OK, December 2010 (in the middle) is indefined and January 2011 is OK as well. Which conditional do I add for this case? Any ideas anybody? Thanks.

look here http://www.ltornado.com/calendarlt.html

the code I use is this:

<script type="text/javascript">

var todaydate=new Date()
var curmonth=todaydate.getMonth()+1 //get current month (1-12)
var curyear=todaydate.getFullYear() //get current year

</script>

<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="33%">
<script>
if (curmonth - 1 < 1) {
curmonth = curmonth + 12;
curyear = curyear - 1;
}
document.write(buildCal(curmonth-1,curyear, "main", "month", "daysofweek", "days", 1));
</script></td>
<td width="33%">
<script>
if (curmonth +1 > 12) {
curmonth = curmonth - 12;
curyear = curyear + 1;
}
document.write(buildCal(curmonth,curyear, "main", "month", "daysofweek", "days", 1));
</script></td>
<td width="34%">
<script>
if (curmonth +1 > 12) {
curmonth = curmonth - 12;
curyear = curyear + 1;
}
document.write(buildCal(curmonth+1 ,curyear, "main", "month", "daysofweek", "days", 1));
</script></td>
</tr>
</table>
<!--end calendar script-->

Den7
12-14-2010, 09:16 AM
That was a little silly of me to ask.. :cool: The problem was in a conditional for the month in the middle... I simply removed it as it was not required there at all. However, it raised another question as to why would it stop the code execution and perform it erroneously if it is not used? This "ballast" was not supposed to be in a way was it?