增加天数+1在15/12之后不起作用 [英] Increase days +1 dont work after 15/12

查看:64
本文介绍了增加天数+1在15/12之后不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在显示一些日期,从今天12-11开始每按一次按钮我增加+1,所以我接下来他们,当它的时候星期六或星期天我想增加更高然后+1所以我不需要按3次按钮来到15/12因为13,14十二月是星期六和星期日

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

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);
      }



}





现在它增加+ 1如果他们不是星期六或星期日,但我只能这样吗

11 dec

12 dec

13 dec(未显示)

14 dec(未显示)

15 dec

但是在这里尝试再次点击按钮时我需要按3次按钮16月12日出现=?为什么?



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?

推荐答案

正如Ryan所说,如果你调试你的代码,你会看到到底发生了什么,原因应该是显而易见的:



首先点击:

增加= 1

toTime =(11 + 1)= 12月12日

结果:12月12日。



第二次点击:

增加= 2

toTime =(11 + 2)= 12月13日

这是星期六,所以再加两天。

结果:12月15日



第三次点击:

增加= 3

toTime =(11 + 3)= 12月14日

这是一个星期天,所以加一天。

结果:12月15日



第四次点击:

增加= 4

toTime =(11 + 4)= 12月15日

结果: 12月15日



第五次点击:

增加= 5

toTime =(11 + 5)= 12月16日

结果:12月16日





如果你在星期六/星期日加上额外的天数,你就忘记了更新增加变量。
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.


这篇关于增加天数+1在15/12之后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆