Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am displaying some dates, from today 12-11 on every button click i increase with +1 so i get next they, and when its saturday or sunday i want to increase higher then +1 so i dont dont need to press the button 3 times to come to the 15/12 because 13,14 dec are saturday and sunday
JavaScript
var increase=0;
function IncreaseDays()
{ 
var toTime= new Date()
        increase = increase + 1;
        toTime.setDate(toTime.getDate() + increase);
    
    
        if (toTime.getDay() == 0) {

        toTime.setDate(toTime.getDate() + 1);
        }
     else if(toTime.getDay() == 6) {
 
      toTime.setDate(toTime.getDate() + 2);
      }



}


Right now it adds +1 if the they is not saturday or sunday, but i only get like this
11 dec
12 dec
13 dec (not displayed)
14 dec (not displayed)
15 dec
But when here trying to click the button again i need to press the button 3 times before 16 dec shows up=? why?
Posted
Comments
ZurdoDev 11-Dec-14 8:06am    
If you debug the code you'll see exactly what is happening.
Kurac1 11-Dec-14 8:10am    
And if i see, but don't understand whats happening? what should i do then?
Thanks7872 11-Dec-14 8:13am    
Than you need to explain what you see and doubt with the same. Further, you have already posted this once. Don't repost questions. Its not allowed. Don't tell me this is different one.
Kurac1 11-Dec-14 8:16am    
Before u start talking go and look and my question before saying anything
Kurac1 11-Dec-14 8:33am    
after pressing button and going to 15/12 it then goes in getSunday and there for on click 16/12 is not displayed

1 solution

As Ryan said, if you debug your code, you'll see exactly what is happening, and the reason should be obvious:

First click:
increase = 1
toTime = (11 + 1) = 12th December
Result: 12th December.

Second click:
increase = 2
toTime = (11 + 2) = 13th December
It's a Saturday, so add two days.
Result: 15th December

Third click:
increase = 3
toTime = (11 + 3) = 14th December
It's a Sunday, so add one day.
Result: 15th December

Fourth click:
increase = 4
toTime = (11 + 4) = 15th December
Result: 15th December

Fifth click:
increase = 5
toTime = (11 + 5) = 16th December
Result: 16th December


When you add the extra days to skip Saturday / Sunday, you've forgotten to update the increase variable.
 
Share this answer
 
Comments
Kurac1 11-Dec-14 9:05am    
10 dec increase gets 1 displays 11dec
11 dec increase gets 2 displays 12dec
13 dec increase gets 3 goes in to else if(toTime.getDay() == 6) {

toTime.setDate(toTime.getDate() + 2);
}
Display 15Dec on click on dec to display 16 dec it has increase 4 and goes in to if (toTime.getDay() == 0) {

toTime.setDate(toTime.getDate() + 1);
}
on next click increase gets 5 and nothing happens,
On next klick increase gets 6 and are then displayed
Kurac1 11-Dec-14 9:12am    
How could i update 'When you add the extra days to skip Saturday / Sunday, you've forgotten to update the increase variable.'
Richard Deeming 11-Dec-14 9:15am    
Seriously?!

When you add days to your toTime, you need to update the increase variable to match. It's not rocket science!
Kurac1 11-Dec-14 9:17am    
Good answer ;)
Kurac1 11-Dec-14 9:42am    
Seriously?!
How do u mean?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900