Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use this code on button click,
i am checking if day are saturday or sunday,
And if it is, increase by +3 day so from saturday to monday 3 days,
it steps over the days on click but it also add 4 more days to it why??
if its not saturday och sunday it should add +1 day

JavaScript
var increase=0;
function IncreaseDays()
{ 
        var toTime=new Date();

        increase = increase + 1;
        toTime.setDate(toTime.getDate() + increase);
    if (toTime.getDay() == 0 || toTime.getDay() == 6) {

        increase = increase + 3;
        toTime.setDate(toTime.getDate() + increase);
    }



}
Posted

Because you first add one and then add another 3. Your code indents but there is no actual if. There is also more logic to it than you now have. For a nice example about business days, check this link:
http://javascript.about.com/library/blbusdayadd.htm[^]

You might also want to consider a library like datejs:
https://code.google.com/p/datejs/[^]

Good luck!
 
Share this answer
 
Eh, sorry, didn't notice this is javascript :( Solution updated

I would advise you to use moment.js, date.js or something.

You're doing the following (say it is last saturday)
set toTime to last weekend (6. december, saturday)
increase = 1
increase toTime by one to 7.12. (sunday)

increase = 4 (increase +3)
toTime = 7.12 +4 = 11.12 (thursday)

Now the solution:
function IncreaseDays() {
var toTime = new Date(); //set date to Today
int day = toTime.getDay(); // get weekDay
var dayInMonth = toTime.getDate(); // get dayInMonth
// if sunday, increase the date by one
if (day == 0) {
toTime.setDate(dayInMonth +1);
} // if saturday, increase the date by 2
elseif (day == 6)
{
toTime.setDate(dayInMonth +2);
}
}

You might want to test this if the date is near the end of the month...
If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
v2
Comments
Kurac1 11-Dec-14 6:46am    
It dont work nothing happens
Kurac1 11-Dec-14 6:50am    
What about the days monday to friday? will they be displayed? in my case not.
Sinisa Hajnal 11-Dec-14 7:05am    
How are you using it? Where are you setting the value of toTime?
Kurac1 11-Dec-14 7:14am    
From New Date object, var result = listItemCustom + "T" + listItemtoTime + ":00" + "Z"; this 2 variabels are my datepicker and timepicker
Sinisa Hajnal 15-Dec-14 3:03am    
Does your toTime hold correct time? Did you check with "console.log(result);"

Also, you didn't show me how you use the function.

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