1. ## Strange Behavior

I have got some strange behavior from a script.

I am making myself a personal diary script that is a calendar and so far all is going well except for the months aspect of the script. The interface will eventually be linked to a database, that however is a long way off until I get the front end behaving itself, I can't progress.

First off I have to describe the problem...

I have four buttons, 1 set (two of the four) for advancing and regressing the years, works perfectly fine, not a problem with that.

The second set of buttons is for the months. The calendar is designed to start from the current month, so the page loads, in this instance the month being October shows up and when I click the advance month button, it moves to November, December then instead of rolling over in to January and advancing the year, it goes back to October but in the next year...

Going in the opposite direction, similar happens, instead of December and the year regressing when January is reached, it goes back to October but the year will be October of the previous year.

I figure its a case of can't see the wood for the trees, so here is the actual logic part of the of the script.

Code:
```function buttonClick(o){

switch(o.value){
// year down
case "previous year": y--;	break;
// year up
case "next year": y++;	break;
// month down
case "previous month": m = (m>=0)? m-1:11; y = (m==0)? y-1 : y;	break;
// month up
case "next month": m = (m<=11)? m+1:0; y = (m==0)? y+1 : y;	break;

}

}```
The year buttons and month buttons are attached to the script button click and as soon as a change is made, the display up dates.

Now...
Code:
```// year down
case "previous year": y--;	break;
// year up
case "next year": y++;	break;```
works just fine and dandy... the problem is in this this part
Code:
```// month down
case "previous month": m = (m>=0)? m-1:11; y = (m==0)? y-1 : y;	break;
// month up
case "next month": m = (m<=11)? m+1:0; y = (m==0)? y+1 : y;	break;```
Any ideas...

2. Registered User
Join Date
Mar 2009
Posts
590
Shouldn't
Code:
`case "previous month": m = (m>=0)? m-1:11; y = (m==0)? y-1 : y;	break;`
be

Code:
`case "previous month": m = (m>=0)? m-1:11; y = (m==11)? y-1 : y;	break;`
and
Code:
`  case "next month": m = (m<=11)? m+1:0; y = (m==0)? y+1 : y;	break;`
be

Code:
`  case "next month": m = (m<=10)? m+1:0; y = (m==0)? y+1 : y;	break;`

3. I think that I will just live with it by adding a year up and year down button and work on the month thing fixed so it goes from what ever up and down but no roll around.

Like I said, its a personal project not for public release, basically a diary for me to put my appointments in to.

Thanks for trying but no cigar as the saying goes.

4. Registered User
Join Date
Mar 2009
Posts
590
Sorry for my inferior coding. I promise you I won't bother you in the future with it.

5. Registered User
Join Date
Sep 2007
Posts
390
If I understand
Code:
```
<script type="text/javascript">

var d = new Date();

var m = d.getMonth()

var nextMonth = (m + 1) % 12 ;

var previousMonth = ( m + 11) % 12;

</script>```

6. Ayşe, what I was trying to do was to make a forward and back button that would allow me to move back one month at a time or forward and the year to roll over when months regressed , eg, Feb 2013, Jan 2013, Dec 2012 ... and like wise forwards Nov 2013, Dec 2013, Jan 2014

What was happening was the month for example were in October to be able to advance Oct 2013, Nov 2013, Dec 2013, Jan 2014 and what was actually happening was the calendar was going Oct 2013, Nov 2013, Dec 2013, Oct 2014 and similar when regressing.

I have since decided to make the script so that I roll the years manually then the months to what I want, as this is for my personal use its not that big a hardship to live with, had this been a public project or for a client then I would have tried solving the issue.

For those who have tried to help, thanks. Its just a bizarre thing thats happening to the display that I really wanted to check if it was my logic or the script engine as I have already had one incident that turned out to be the script engine but this activity is persistent, therefore the code logic is flawed.