选择日期后添加天数,周末和节假日除外 [英] Add days after date is selected excluding weekends and holidays

查看:27
本文介绍了选择日期后添加天数,周末和节假日除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本字段.在第一个文本字段中,我使用 jQueryUI Datepicker 添加了一个日历.当用户从日期选择器中选择日期时,它应该自动将 30 天添加到日期并在第二个文本字段中设置此值.我已经开始工作以将日期添加 30 天.

I have two text fields. In the first textfield I have added a calendar using jQueryUI Datepicker. When the user selects the date from the datepicker, it should automatically add 30 days to the date and set this value in the second textfield. I've got this working to add 30 days to the date.

但是,它应该排除周末和节假日并且只显示 30 个工作日.我该怎么做?

However, it should exclude weekends and holidays and display only 30 business days. How do I do this?

if ($('#cat option:selected').text() == "ABC") {
    var date_billed = $('#datebilled').datepicker('getDate');
    var date_overdue = new Date();
    date_overdue.setDate(date_billed.getDate() + 30);
    date_overdue = $.datepicker.formatDate('mm/dd/yy', date_overdue);
    $('#datepd').val(date_overdue).prop('readonly', true);
}

更新:我添加了函数 noWeekendsOrHolidays 来禁用周末或假期.无论如何,我可以使用此功能来计算日期并将工作日添加到日期中吗?

Update: I have added the function noWeekendsOrHolidays to disable weekends or holidays. Is there anyway I can use this function to calculate and add business days to the date?

//holidays
            var natDays = [
              [7, 4, 'us']
            ];

            function noWeekendsOrHolidays(date) {
                var noWeekend = $.datepicker.noWeekends(date);
                if (noWeekend[0]) {
                    return nationalDays(date);
                } else { 
                    return noWeekend;
                }
            }
            function nationalDays(date) {
                for (i = 0; i < natDays.length; i++) {
                    if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
                        return [false, natDays[i][2] + '_day'];
                    }
                }
                return [true, ''];
            }

推荐答案

解决了!这个答案这里有很大帮助.

Solved it! This answer here helped tremendously.

代码:

function AddBusinessDays(weekDaysToAdd) {
      var curdate = new Date();
      var realDaysToAdd = 0;
      while (weekDaysToAdd > 0){
        curdate.setDate(curdate.getDate()+1);
        realDaysToAdd++;
        //check if current day is business day
        if (noWeekendsOrHolidays(curdate)[0]) {
          weekDaysToAdd--;
        }
      }
      return realDaysToAdd;

    }


var date_billed = $('#datebilled').datepicker('getDate');
var date_overdue = new Date();
var weekDays = AddBusinessDays(30);
date_overdue.setDate(date_billed.getDate() + weekDays);
date_overdue = $.datepicker.formatDate('mm/dd/yy', date_overdue);
$('#datepd').val(date_overdue).prop('readonly', true);

这篇关于选择日期后添加天数,周末和节假日除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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