使用JavaScript计算工作日内的预期交货日期(计算假期)? [英] Calculate an expected delivery date (accounting for holidays) in business days using JavaScript?
问题描述
在重新访问此脚本并进行一些修改之后,可以使用以下内容允许用户添加计算预期交付日期的功能.
After revisiting this script, and some modifications, the following is available to allow a user to add a feature that calculates the expected delivery date.
// 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 );
订购日期:2020-02-12,交货日期:2020-02-25
Order date: 2020-02-12, Delivery date: 2020-02-25
编写原型是为了允许来自日期类型输入的表单(HTML5表单)输入,因为它们已经是ISO YYYY-MM-DD格式,并且如果需要更新特定字段,则将输出格式化为此类.
The prototype is written to allow inputs from forms (HTML5 forms) of date type inputs as they are already in an ISO YYYY-MM-DD format and the output is formatted as such should that be needing to update a particular field.
典型用途是...
var delDate = new Date( ISOdate ).businessDays( addBusinessDays, holidayData );
其中delDate是ISO格式的日期,例如2020-01-01
where the delDate is an ISO format date, eg, 2020-01-01
推荐答案
感谢您的投入,我对自己正在采用的方法进行了长时间的重新思考,并提出了这个小数目...
Thanks for your input guys, I had a long hard re-think over the approach I was making for this and came up with this little number...
var businessDays = 7, counter = 0; // set to 1 to count from next business day
while( businessDays>0 ){
var tmp = new Date();
var startDate = new Date();
tmp.setDate( startDate .getDate() + counter++ );
switch( tmp.getDay() ){
case 0: case 6: break;// sunday & saturday
default:
businessDays--;
};
}
想法是从工作日开始,然后在遇到工作日范围内遇到的每一天倒数至零.这种开关的使用将使一个人可以将一周中的某一天宣布为非工作日,例如某人可能不在星期一工作,因此,case:1的增加将包括一个星期一.
The idea was to start with the business days and count backwards to zero for each day encountered that fell in to the range of a business day. This use of switch would enable a person to declare a day in the week as a non-business day, for example someone may not work on a monday, therefore the addition of case:1 would include a monday.
这是一个简单的脚本,不会考虑到公众假期或银行假期,因此需要使用更复杂的脚本.
This is a simple script and does not take in to account public or bank holidays, that would be asking for a much more complex script to work with.
结果是将日期设置为发货日期,然后用户可以按照自己喜欢的任何格式提取日期信息,例如.
The result is a date that is set to the date of shipping, the user can then extract the date info in any format that they please, eg.
var shipDate = tmp.toUTCString().slice(1,15);
这篇关于使用JavaScript计算工作日内的预期交货日期(计算假期)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!