Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 交货日期不包括周末_Javascript_Date - Fatal编程技术网

Javascript 交货日期不包括周末

Javascript 交货日期不包括周末,javascript,date,Javascript,Date,我目前正在使用这个脚本 <script type="text/javascript"> var _next = new Date(new Date() * 1 + 24*60*60*1000*9); document.write(" Your expected delivery date is " + (_next.getMonth() + 1) + "/" + _next.getDate() + "/" + _next.getFullYear()); </script>

我目前正在使用这个脚本

<script type="text/javascript">
var _next = new Date(new Date() * 1 + 24*60*60*1000*9);
document.write(" Your expected delivery date is " + (_next.getMonth() + 1) + "/" + _next.getDate() + "/" + _next.getFullYear());
</script>  

var_next=新日期(新日期()*1+24*60*60*1000*9);
document.write(“您的预期交付日期是”+(_next.getMonth()+1)+“/”+_next.getDate()+“/”+_next.getFullYear());
我想知道,如果可能的话,我如何将周末排除在外。

试试这个:

function deliver(inDays, startingOn){
  var s, f = 0, d;
  if(!inDays)inDays = 0;
  s = !startingOn ? new Date : new Date(startingOn);
  for(var i=0,n,t=0,l=inDays; i<l; i++,t+=86400000){
    n = new Date(s.getTime()+t).getDay();
    if(n === 0 || n === 6)f++;
  }
  d = new Date(s.getTime()+86400000*(inDays+f));
  return 'Your expected delivery date is '+d.toLocaleDateString();
}
// same day delivery
console.log(deliver());
// deliver in 9 days stating today
console.log(deliver(9));
// deliver in 9 days starting on October 12, 2013 - must be a valid Date String
console.log(deliver(9, 'October 12, 2013'));
/* Note that in the last example the Date starts on the weekend, therefore same
   day becomes Monday, which if you don't work weekends is the first day you
   would see the order anyways.
*/
功能交付(在天,开始){
var s,f=0,d;
如果(!inDays)inDays=0;
s=!开始?新日期:新日期(开始);
对于(变量i=0,n,t=0,l=in天;i
如果您愿意,只需增加小时数。这是一个预计在下一个工作日交付的代码

function myFunction() { 
var d = new Date();
var weekday = new Array(7);
weekday[0] = "sunday";
weekday[1] = "monday";
weekday[2] = "tuesday";
weekday[3] = "wednesday";
weekday[4] = "thursday";
weekday[5] = "friday";
weekday[6] = "saturday";
var monthday = new Array(12);
monthday[0] = "01";
monthday[1] = "02";
monthday[2] = "03";
monthday[3] = "04";
monthday[4] = "05";
monthday[5] = "06";
monthday[6] = "07";
monthday[7] = "08";
monthday[8] = "09";
monthday[9] = "10";
monthday[10] = "11";
monthday[11] = "12";


if (d.getDay() > 0 && d.getDay() <= 4) {
    d.setHours(d.getHours() + 24);
    var deliver = weekday[d.getDay()];
    var day = d.getDate();
    var month = monthday[d.getMonth()];
    var year = d .getFullYear();
    document.getElementById("deliveryday").innerHTML = deliver + ' ' + day + '-' + month + '-' + year;
} else if (d.getDay() == 0) {
    d.setHours(d.getHours() + 48);
    var deliver = weekday[d.getDay()];
    var day = d.getDate();
    var month = monthday[d.getMonth()];
    var year = d .getFullYear();
    document.getElementById("deliveryday").innerHTML = deliver + ' ' + day + '-' + month + '-' + year;
} else {
    d.setHours(d.getHours() + 72);
    var deliver = weekday[d.getDay()];
    var day = d.getDate();
    var month = monthday[d.getMonth()];
    var year = d .getFullYear();
    document.getElementById("deliveryday").innerHTML = deliver + ' ' + day + '-' + month + '-' + year;
}
}
myFunction()
函数myFunction(){
var d=新日期();
var weekday=新数组(7);
工作日[0]=“周日”;
工作日[1]=“周一”;
工作日[2]=“星期二”;
工作日[3]=“星期三”;
工作日[4]=“周四”;
工作日[5]=“周五”;
工作日[6]=“周六”;
var monthday=新阵列(12);
monthday[0]=“01”;
monthday[1]=“02”;
monthday[2]=“03”;
monthday[3]=“04”;
monthday[4]=“05”;
monthday[5]=“06”;
monthday[6]=“07”;
monthday[7]=“08”;
monthday[8]=“09”;
monthday[9]=“10”;
monthday[10]=“11”;
monthday[11]=“12”;
如果(d.getDay()>0和&d.getDay()

我写了一个例程,你提供一个日期对象,应用方法,一个单独的列表中的公共假日被传递,任何假日/公共假日包括周末都被跳过

是这个职位吗

// array of ISO YYYY-MM-DD format dates
publicHolidays = {
  uk:["2020-01-01","2020-04-10","2020-04-13","2020-05-08","2020-05-25",
    "2020-08-03","2020-08-31","2020-12-25","2020-12-28"],
   usa:["2020-01-01","2020-01-20","2020-02-14","2020-02-17","2020-04-10",
    "2020-04-12","2020-05-10","2020-05-25","2020-06-21","2020-07-03",
    "2020-07-04","2020-09-07","2020-10-12","2020-10-31","2020,11,11",
    "2020-11-26","2020-12-25"]
}
// check if there is a match in the array
Date.prototype.isPublicHoliday = function( data ){// we check for a public holiday
    if(!data) return 1;
return data.indexOf(this.toISOString().slice(0,10))>-1? 0:1;
}

// calculation of business days
Date.prototype.businessDays = function( d, holidays ){
    var holidays = holidays || false, t = new Date( this ); // copy date.  
    while( d ){ // we loop while d is not zero...       
    t.setDate( t.getDate() + 1 ); // set a date and test it
    switch( t.getDay() ){ // switch is used to allow easier addition of other days of the week
            case 0: case 6: break;// sunday & saturday
            default: // check if we are a public holiday or not
            d -= t.isPublicHoliday( holidays ); 
        } 

    }
    return t.toISOString().slice(0,10); // just the YYY-MM-DD 
}

// dummy var, could be a form field input
OrderDate = "2020-02-12";
// test with a UK holiday date
var deliveryDate = new Date(OrderDate).businessDays(7, publicHolidays.usa);
// expected output 2020-02-25
console.log("Order date: %s, Delivery date: %s",OrderDate,deliveryDate );

您的代码当前将在当前日期后添加9天,而不考虑一周中的当前日期。您希望执行什么操作?(例如,将9天添加到今天,然后查找之后的下一个工作日;或者,将9个工作日添加到今天)这个周末我一直在做这件事,这是我在5天以下的时候所做的,看起来很好,不确定我现在哪里出了问题,非常感谢你的帮助。
// array of ISO YYYY-MM-DD format dates
publicHolidays = {
  uk:["2020-01-01","2020-04-10","2020-04-13","2020-05-08","2020-05-25",
    "2020-08-03","2020-08-31","2020-12-25","2020-12-28"],
   usa:["2020-01-01","2020-01-20","2020-02-14","2020-02-17","2020-04-10",
    "2020-04-12","2020-05-10","2020-05-25","2020-06-21","2020-07-03",
    "2020-07-04","2020-09-07","2020-10-12","2020-10-31","2020,11,11",
    "2020-11-26","2020-12-25"]
}
// check if there is a match in the array
Date.prototype.isPublicHoliday = function( data ){// we check for a public holiday
    if(!data) return 1;
return data.indexOf(this.toISOString().slice(0,10))>-1? 0:1;
}

// calculation of business days
Date.prototype.businessDays = function( d, holidays ){
    var holidays = holidays || false, t = new Date( this ); // copy date.  
    while( d ){ // we loop while d is not zero...       
    t.setDate( t.getDate() + 1 ); // set a date and test it
    switch( t.getDay() ){ // switch is used to allow easier addition of other days of the week
            case 0: case 6: break;// sunday & saturday
            default: // check if we are a public holiday or not
            d -= t.isPublicHoliday( holidays ); 
        } 

    }
    return t.toISOString().slice(0,10); // just the YYY-MM-DD 
}

// dummy var, could be a form field input
OrderDate = "2020-02-12";
// test with a UK holiday date
var deliveryDate = new Date(OrderDate).businessDays(7, publicHolidays.usa);
// expected output 2020-02-25
console.log("Order date: %s, Delivery date: %s",OrderDate,deliveryDate );