Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   Strange getMonth/setMonth problem (http://www.programmingforums.org/showthread.php?t=11719)

aaroncampbell Oct 27th, 2006 11:45 AM

Strange getMonth/setMonth problem
 
1 Attachment(s)
Ok, the code is attached, and you can use it by adding onfocus="cal.showCal(this);" to any text input like this:
:

<input name="demo" type="text" maxlength="10" size="10" onfocus="cal.showCal(this);" />
Now, The problem I have it that the month down button doesn't work, and the month up button goes 2 months at a time...except from Dec to Jan...which it does correctly.

Anyway, It sounds easy, but I tried manually changing the adjustment in the Calendar.prototype.changeMonth function like this:
:

this.current.setMonth(this.current.getMonth()+1);
And it still goes by 2. So I tried this:
:

this.current.setMonth(this.current.getMonth());
And it doesn't change (as expected), so I tried this:
:

this.current.setMonth(this.current.getMonth()-1);
And it doesn't change!!! How can this.current.getMonth()-1 == this.current.getMonth()!!!

Anyway, the problem is that creating a test case didn't seem to work...I couldn't repeat the glitch. Maybe someone can find my glaring error? I know that this system worked not that long ago...I HAVE made changes, but I have no idea where this went wrong...

DaWei Oct 27th, 2006 4:23 PM

I find two references to "setMonth" in the code, both invocations. Where's the function definition?

aaroncampbell Oct 27th, 2006 4:31 PM

http://developer.mozilla.org/en/docs...:Date:setMonth
It's part of the Date object.

aaroncampbell Oct 31st, 2006 2:04 PM

1 Attachment(s)
I've used alerts, and console.logs all over the place, and I'm still lost. One thing I DO know is that the function isn't running twice. It only runs once. I log the adjustment (1 or -1 as expected), I run getMonth (0-11, and always as expected), I log the math (-1 - 12, and again as expected), and then I setMonth, and log getMonth...and it's OFF!

Summary: I'm still lost.

I'm attaching 3 files... the .js, the .css, and a .html that will let you test it.

aaroncampbell Oct 31st, 2006 2:49 PM

As it turns out, the date that I was adjusting was Month/LastDayOfMonth/Year, so it would be something like: 10/31/2006 and it would try to change it to 11/31/2006 (which doesn't exist, 11 only has 30 days). It doesn't know what to do with that, so it interprets it as 12/01/2006. I simply added this.current.setDate(1); before the setMonth and it worked perfectly.


All times are GMT -5. The time now is 2:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC