代码之家  ›  专栏  ›  技术社区  ›  user3217031

如何查找即将到来的工作日?

  •  2
  • user3217031  · 技术社区  · 10 年前

    如何设置Bootstrap日期选择器 endDate 到下周的星期一,所以每次打开日历时,无论今天是星期几,都应该设置 实际结束 到下一个星期一,所有日期都应该在那一天之后禁用?

    我试图使用endDate:“+1w”,但7天后禁用。

    $(".date-picker").datepicker({
        autoclose: true,
        startDate: "01/01/2014",
        endDate: "+1w",
        format: "dd M yyyy"
    });
    
    2 回复  |  直到 10 年前
        1
  •  0
  •   Community CDub    7 年前

    下周一

    你的问题很有趣。所以我试图找出答案,但我无法找出一个简单的逻辑。意思是当我在C中遇到类似的问题时# "Datetime - Get next tuesday"

    在这里,Jon Skeet已经整理了一个简单的逻辑,以便在下周找到答案。

    //consider date as April 1, 2014 (TUESDAY)
    var daysUntilMonday = (1 - date.getDay() + 7) % 7;
    

    说明:

    这里1(表示星期一,因为在数组中考虑天数)

    //(…+7)%7确保我们最终得到范围[0, 6] (Jon引用)


    现在只需添加这些日期并将其设置为

    date.setDate(date.getDate() + daysUntilMonday );
    

    完整代码:

      $('#datepicker').datepicker({
          autoclose: true,
          startDate: "01/04/2014",
          endDate: nextMonday("01/04/2014"),
          format: "dd M yyyy"
      });
    
      function nextMonday(theDate) {
          var dat = theDate.split("/");
          var date = new Date(dat[2], (+dat[1]) - 1, dat[0]);
          var daysUntilMonday = (1 - date.getDay() + 7) % 7;
          date.setDate(date.getDate() + daysUntilMonday);
          return date;
      }
    

    JSFiddle

    财政年度: 如果日期已经是星期一,会发生什么?检查一下。。

        2
  •  0
  •   user3217031 user3217031    10 年前

    我自己解决了问题:

    var current_day = new Date();
        if(current_day.getDay() == 0){
            $(".date-picker").datepicker({
                autoclose: true,
                startDate: "01/01/2014",
                endDate: "+0d",
                format:    "dd M yyyy"
            });
        }else if(current_day.getDay() == 1){
            $(".date-picker").datepicker({
                autoclose: true,
                startDate: "01/01/2014",
                endDate: "+6d",
                format:    "dd M yyyy"
            });
        }else if(current_day.getDay() == 2){
            $(".date-picker").datepicker({
                autoclose: true,
                startDate: "01/01/2014",
                endDate: "+5d",
                format:    "dd M yyyy"
            });
        }else if(current_day.getDay() == 3){
            $(".date-picker").datepicker({
                autoclose: true,
                startDate: "01/01/2014",
                endDate: "+4d",
                format:    "dd M yyyy"
            });
        }else if(current_day.getDay() == 4){
            $(".date-picker").datepicker({
                autoclose: true,
                startDate: "01/01/2014",
                endDate: "+3d",
                format:    "dd M yyyy"
            });
        }else if(current_day.getDay() == 5){
            $(".date-picker").datepicker({
                autoclose: true,
                startDate: "01/01/2014",
                endDate: "+2d",
                format:    "dd M yyyy"
            });
        }else{
            $(".date-picker").datepicker({
                autoclose: true,
                startDate: "01/01/2014",
                endDate: "+1d",
                format:    "dd M yyyy"
            });
        }