function addWorkDays(date, days, locale= "en-US") {
date=new Date(date);
let w = Array.isArray(locale)?locale:new Intl.Locale(locale).weekInfo.weekend,
dir = days<0?-1:1;
days = Math.abs(days);
w.at(-1)==7 && (w[w.length-1]=0);
while (days) !w.includes(date.getUTCDay(date.setUTCDate(date.getUTCDate()+dir))) && days--;
return date;
}
//=========================================
// Test Cases
//=========================================
//========================================
// Test with Sat-Sun as weekend default
// uses locale 'en-US' for USA by default
//========================================
var r=0; // test tracker
r |=test(1,"2023-11-27",1,new Date("2023-11-28"));
r |=test(2,"2023-11-27",2,new Date("2023-11-29"));
r |=test(3,"2023-11-27",3,new Date("2023-11-30"));
r |=test(4,"2023-11-27",4,new Date("2023-12-01"));
r |=test(5,"2023-11-27",5,new Date("2023-12-04"));
r |=test(6,"2023-11-27",6,new Date("2023-12-05"));
r |=test(7,"2023-11-27",7,new Date("2023-12-06"));
r |=test(8,"2023-11-27",8,new Date("2023-12-07"));
r |=test(9,"2023-11-27",9,new Date("2023-12-08"));
r |=test(10,"2023-12-02",1,new Date("2023-12-04"));
r |=test(11,"2023-12-02",2,new Date("2023-12-05"));
r |=test(12,"2023-12-02",3,new Date("2023-12-06"));
r |=test(13,"2023-12-03",1,new Date("2023-12-04"));
r |=test(14,"2023-12-03",2,new Date("2023-12-05"));
r |=test(15,"2021-02-01",2,new Date("2021-02-03"));
r |=test(16,"2021-02-01",3,new Date("2021-02-04"));
r |=test(17,"2021-02-01",4,new Date("2021-02-05"));
r |=test(18,"2021-02-01",5,new Date("2021-02-08"));
r |=test(19,"2021-02-01",365,new Date("2022-06-27"));
r |=test(20,"2021-02-01",100,new Date("2021-06-21"));
r |=test(21,"2021-02-01",20000,new Date("2097-09-30"));
r |=test(24,"2023-12-01",-1,new Date("2023-11-30"));
r |=test(25,"2023-12-01",-2,new Date("2023-11-29"));
r |=test(26,"2023-12-01",-3,new Date("2023-11-28"));
r |=test(27,"2023-12-01",-4,new Date("2023-11-27"));
r |=test(28,"2023-12-01",-365,new Date("2022-07-08"));
r |=test(29,"2023-12-01",-100,new Date("2023-07-14"));
r |=test(30,"2023-12-01",-999,new Date("2020-02-03"));
r |=test(31,"2023-12-02",-1,new Date("2023-12-01"));
r |=test(32,"2023-12-02",-2,new Date("2023-11-30"));
r |=test(33,"2023-12-02",-3,new Date("2023-11-29"));
r |=test(34,"2023-12-02",-4,new Date("2023-11-28"));
r |=test(35,"2023-12-02",-365,new Date("2022-07-11"));
r |=test(36,"2023-12-02",-100,new Date("2023-07-17"));
r |=test(37,"2023-12-02",-999,new Date("2020-02-04"));
r |=test(38,"2023-12-03",-1,new Date("2023-12-01"));
r |=test(39,"2023-12-03",-2,new Date("2023-11-30"));
r |=test(40,"2023-12-03",-3,new Date("2023-11-29"));
r |=test(41,"2023-12-03",-4,new Date("2023-11-28"));
r |=test(42,"2023-12-04",-1,new Date("2023-12-01"));
r |=test(43,"2023-12-04",-2,new Date("2023-11-30"));
r |=test(44,"2023-12-04",-3,new Date("2023-11-29"));
r |=test(45,"2023-12-04",-4,new Date("2023-11-28"));
r |=test(46,"2023-12-04",-5,new Date("2023-11-27"));
r |=test(47,"2023-12-04",-6,new Date("2023-11-24"));
r |=test(48,"2023-12-04",-7,new Date("2023-11-23"));
r |=test(49,"2023-12-04",-8,new Date("2023-11-22"));
if (r==0) console.log("✅ (Sat-Sun) Weekend Passed.");
//========================================
// Test with Fri-Sat as weekend
// use example locale 'ar-SA' for Saudi Arabia
//========================================
var r=0; // test tracker
var locale="ar-SA";
r |=test(1,"2023-11-27",1,new Date("2023-11-28"),locale);
r |=test(2,"2023-11-27",0,new Date("2023-11-27"),locale);
r |=test(3,"2023-12-02",0,new Date("2023-12-02"),locale);
r |=test(4,"2023-12-01",0,new Date("2023-12-01"),locale);
r |=test(5,"2023-11-27",2,new Date("2023-11-29"),locale);
r |=test(6,"2023-11-27",3,new Date("2023-11-30"),locale);
r |=test(7,"2023-11-27",4,new Date("2023-12-03"),locale);
r |=test(8,"2023-11-27",5,new Date("2023-12-04"),locale);
r |=test(9,"2023-11-27",6,new Date("2023-12-05"),locale);
r |=test(10,"2023-11-27",7,new Date("2023-12-06"),locale);
r |=test(11,"2023-11-27",8,new Date("2023-12-07"),locale);
r |=test(12,"2023-11-27",9,new Date("2023-12-10"),locale);
r |=test(13,"2023-12-02",1,new Date("2023-12-03"),locale);
r |=test(14,"2023-12-02",2,new Date("2023-12-04"),locale);
r |=test(15,"2023-12-02",3,new Date("2023-12-05"),locale);
r |=test(16,"2023-12-03",1,new Date("2023-12-04"),locale);
r |=test(17,"2023-12-03",2,new Date("2023-12-05"),locale);
r |=test(18,"2021-02-01",2,new Date("2021-02-03"),locale);
r |=test(19,"2021-02-01",3,new Date("2021-02-04"),locale);
r |=test(20,"2021-02-01",4,new Date("2021-02-07"),locale);
r |=test(21,"2021-02-01",5,new Date("2021-02-08"),locale);
r |=test(22,"2021-02-01",365,new Date("2022-06-27"),locale);
r |=test(23,"2021-02-01",100,new Date("2021-06-21"),locale);
r |=test(24,"2021-02-01",20000,new Date("2097-09-30"),locale);
r |=test(25,"2023-12-01",-1,new Date("2023-11-30"),locale);
r |=test(26,"2023-12-01",-2,new Date("2023-11-29"),locale);
r |=test(27,"2023-12-01",-3,new Date("2023-11-28"),locale);
r |=test(28,"2023-12-01",-4,new Date("2023-11-27"),locale);
r |=test(29,"2023-12-01",-365,new Date("2022-07-10"),locale);
r |=test(30,"2023-12-01",-100,new Date("2023-07-16"),locale);
r |=test(31,"2023-12-01",-999,new Date("2020-02-03"),locale);
r |=test(32,"2023-12-02",-1,new Date("2023-11-30"),locale);
r |=test(33,"2023-12-02",-2,new Date("2023-11-29"),locale);
r |=test(34,"2023-12-02",-3,new Date("2023-11-28"),locale);
r |=test(35,"2023-12-02",-4,new Date("2023-11-27"),locale);
r |=test(36,"2023-12-02",-365,new Date("2022-07-10"),locale);
r |=test(37,"2023-12-02",-100,new Date("2023-07-16"),locale);
r |=test(38,"2023-12-02",-999,new Date("2020-02-03"),locale);
r |=test(39,"2023-12-03",-1,new Date("2023-11-30"),locale);
r |=test(40,"2023-12-03",-2,new Date("2023-11-29"),locale);
r |=test(41,"2023-12-03",-3,new Date("2023-11-28"),locale);
r |=test(42,"2023-12-03",-4,new Date("2023-11-27"),locale);
r |=test(43,"2023-12-04",-1,new Date("2023-12-03"),locale);
r |=test(44,"2023-12-04",-2,new Date("2023-11-30"),locale);
r |=test(45,"2023-12-04",-3,new Date("2023-11-29"),locale);
r |=test(46,"2023-12-04",-4,new Date("2023-11-28"),locale);
r |=test(47,"2023-12-04",-5,new Date("2023-11-27"),locale);
r |=test(48,"2023-12-04",-6,new Date("2023-11-26"),locale);
r |=test(49,"2023-12-04",-7,new Date("2023-11-23"),locale);
r |=test(50,"2023-12-04",-8,new Date("2023-11-22"),locale);
if (r==0) console.log("✅ (Fri-Sat) Weekend Passed.");
//========================================
// Test with Thu-Fri as weekend
// use example locale 'ps-AF' for Afghanistan
//========================================
var r=0; // test tracker
var locale="ps-AF";
r |=test(1,"2023-11-27",1,new Date("2023-11-28"),locale);
r |=test(2,"2023-11-27",0,new Date("2023-11-27"),locale);
r |=test(3,"2023-11-30",0,new Date("2023-11-30"),locale);
r |=test(4,"2023-12-01",0,new Date("2023-12-01"),locale);
r |=test(5,"2023-11-27",2,new Date("2023-11-29"),locale);
r |=test(6,"2023-11-27",3,new Date("2023-12-02"),locale);
r |=test(7,"2023-11-27",4,new Date("2023-12-03"),locale);
r |=test(8,"2023-11-27",5,new Date("2023-12-04"),locale);
r |=test(9,"2023-11-27",6,new Date("2023-12-05"),locale);
r |=test(10,"2023-11-27",7,new Date("2023-12-06"),locale);
r |=test(11,"2023-11-27",8,new Date("2023-12-09"),locale);
r |=test(12,"2023-11-27",9,new Date("2023-12-10"),locale);
r |=test(13,"2023-12-02",1,new Date("2023-12-03"),locale);
r |=test(14,"2023-12-02",2,new Date("2023-12-04"),locale);
r |=test(15,"2023-12-02",3,new Date("2023-12-05"),locale);
r |=test(16,"2023-12-03",1,new Date("2023-12-04"),locale);
r |=test(17,"2023-12-03",2,new Date("2023-12-05"),locale);
r |=test(18,"2021-02-01",2,new Date("2021-02-03"),locale);
r |=test(19,"2021-02-01",3,new Date("2021-02-06"),locale);
r |=test(20,"2021-02-01",4,new Date("2021-02-07"),locale);
r |=test(21,"2021-02-01",5,new Date("2021-02-08"),locale);
r |=test(22,"2021-02-01",365,new Date("2022-06-27"),locale);
r |=test(23,"2021-02-01",100,new Date("2021-06-21"),locale);
r |=test(24,"2021-02-01",20000,new Date("2097-09-30"),locale);
r |=test(25,"2021-02-01",-1,new Date("2021-01-31"),locale);
r |=test(26,"2021-02-01",-2,new Date("2021-01-30"),locale);
r |=test(27,"2021-02-01",-3,new Date("2021-01-27"),locale);
r |=test(28,"2021-02-01",-4,new Date("2021-01-26"),locale);
r |=test(29,"2021-02-01",-365,new Date("2019-09-09"),locale);
r |=test(30,"2021-02-01",-100,new Date("2020-09-14"),locale);
r |=test(31,"2021-02-01",-999,new Date("2017-04-04"),locale);
if (r==0) console.log("✅ (Thu-Fri) Weekend Passed.");
//========================================
// Test with Fri as weekend
// use example locale 'fa-IR' for Iran
//========================================
var locale="fa-IR";
var r=0;
r |=test(1,"2023-11-27",1,new Date("2023-11-28"),locale);
r |=test(2,"2023-11-27",2,new Date("2023-11-29"),locale);
r |=test(3,"2023-11-27",3,new Date("2023-11-30"),locale);
r |=test(4,"2023-11-27",4,new Date("2023-12-02"),locale);
r |=test(5,"2023-11-27",5,new Date("2023-12-03"),locale);
r |=test(6,"2023-11-27",6,new Date("2023-12-04"),locale);
r |=test(7,"2023-11-27",7,new Date("2023-12-05"),locale);
r |=test(8,"2023-11-27",8,new Date("2023-12-06"),locale);
r |=test(9,"2023-11-27",9,new Date("2023-12-07"),locale);
r |=test(10,"2023-12-02",1,new Date("2023-12-03"),locale);
r |=test(11,"2023-12-02",2,new Date("2023-12-04"),locale);
r |=test(12,"2023-12-02",3,new Date("2023-12-05"),locale);
r |=test(13,"2023-12-03",1,new Date("2023-12-04"),locale);
r |=test(14,"2023-12-03",2,new Date("2023-12-05"),locale);
r |=test(15,"2021-02-01",2,new Date("2021-02-03"),locale);
r |=test(16,"2021-02-01",3,new Date("2021-02-04"),locale);
r |=test(17,"2021-02-01",4,new Date("2021-02-06"),locale);
r |=test(18,"2021-02-01",5,new Date("2021-02-07"),locale);
r |=test(19,"2021-02-01",365,new Date("2022-04-03"),locale);
r |=test(20,"2021-02-01",100,new Date("2021-05-29"),locale);
r |=test(21,"2021-02-01",20000,new Date("2084-12-20"),locale);
r |=test(22,"2021-02-01",-1,new Date("2021-01-31"),locale);
r |=test(23,"2021-02-01",-2,new Date("2021-01-30"),locale);
r |=test(24,"2021-02-01",-3,new Date("2021-01-28"),locale);
r |=test(25,"2021-02-01",-4,new Date("2021-01-27"),locale);
r |=test(26,"2021-02-01",-365,new Date("2019-12-03"),locale);
r |=test(27,"2021-02-01",-100,new Date("2020-10-07"),locale);
r |=test(28,"2021-02-01",-999,new Date("2017-11-23"),locale);
if (r==0) console.log("✅ (Friday only) Weekend Passed.");
//========================================
// Test with Sunday as weekend
// use example locale 'en-IN' for India
//========================================
var locale="en-IN";
var r=0;
r |=test(1,"2023-11-27",1,new Date("2023-11-28"),locale);
r |=test(2,"2023-11-27",2,new Date("2023-11-29"),locale);
r |=test(3,"2023-11-27",3,new Date("2023-11-30"),locale);
r |=test(4,"2023-11-27",4,new Date("2023-12-01"),locale);
r |=test(5,"2023-11-27",5,new Date("2023-12-02"),locale);
r |=test(6,"2023-11-27",6,new Date("2023-12-04"),locale);
r |=test(7,"2023-11-27",7,new Date("2023-12-05"),locale);
r |=test(8,"2023-11-27",8,new Date("2023-12-06"),locale);
r |=test(9,"2023-11-27",9,new Date("2023-12-07"),locale);
r |=test(10,"2023-12-02",1,new Date("2023-12-04"),locale);
r |=test(11,"2023-12-02",2,new Date("2023-12-05"),locale);
r |=test(12,"2023-12-02",3,new Date("2023-12-06"),locale);
r |=test(13,"2023-12-03",1,new Date("2023-12-04"),locale);
r |=test(14,"2023-12-03",2,new Date("2023-12-05"),locale);
r |=test(15,"2021-02-01",2,new Date("2021-02-03"),locale);
r |=test(16,"2021-02-01",3,new Date("2021-02-04"),locale);
r |=test(17,"2021-02-01",4,new Date("2021-02-05"),locale);
r |=test(18,"2021-02-01",5,new Date("2021-02-06"),locale);
r |=test(19,"2021-02-01",365,new Date("2022-04-02"),locale);
r |=test(20,"2021-02-01",100,new Date("2021-05-28"),locale);
r |=test(21,"2021-02-01",20000,new Date("2084-12-20"),locale);
r |=test(22,"2021-02-01",-1,new Date("2021-01-30"),locale);
r |=test(23,"2021-02-01",-2,new Date("2021-01-29"),locale);
r |=test(24,"2021-02-01",-3,new Date("2021-01-28"),locale);
r |=test(25,"2021-02-01",-4,new Date("2021-01-27"),locale);
r |=test(26,"2021-02-01",-365,new Date("2019-12-03"),locale);
r |=test(27,"2021-02-01",-100,new Date("2020-10-07"),locale);
r |=test(28,"2021-02-01",-999,new Date("2017-11-23"),locale);
if (r==0) console.log("✅ (Sunday only) Weekend Passed.");
//========================================
// Test with Sat as weekend
// use example array day 6 [6]
//========================================
var locale=[6];
var r=0;
r |=test(1,"2023-11-27",1,new Date("2023-11-28"),locale);
r |=test(2,"2023-11-27",2,new Date("2023-11-29"),locale);
r |=test(3,"2023-11-27",3,new Date("2023-11-30"),locale);
r |=test(4,"2023-11-27",4,new Date("2023-12-01"),locale);
r |=test(5,"2023-11-27",5,new Date("2023-12-03"),locale);
r |=test(6,"2023-11-27",6,new Date("2023-12-04"),locale);
r |=test(7,"2023-11-27",7,new Date("2023-12-05"),locale);
r |=test(8,"2023-11-27",8,new Date("2023-12-06"),locale);
r |=test(9,"2023-11-27",9,new Date("2023-12-07"),locale);
r |=test(10,"2023-12-02",1,new Date("2023-12-03"),locale);
r |=test(11,"2023-12-02",2,new Date("2023-12-04"),locale);
r |=test(12,"2023-12-02",3,new Date("2023-12-05"),locale);
r |=test(13,"2023-12-03",1,new Date("2023-12-04"),locale);
r |=test(14,"2023-12-03",2,new Date("2023-12-05"),locale);
r |=test(15,"2021-02-01",2,new Date("2021-02-03"),locale);
r |=test(16,"2021-02-01",3,new Date("2021-02-04"),locale);
r |=test(17,"2021-02-01",4,new Date("2021-02-05"),locale);
r |=test(18,"2021-02-01",5,new Date("2021-02-07"),locale);
r |=test(19,"2021-02-01",365,new Date("2022-04-03"),locale);
r |=test(20,"2021-02-01",100,new Date("2021-05-28"),locale);
r |=test(21,"2021-02-01",20000,new Date("2084-12-20"),locale);
r |=test(22,"2021-02-01",-1,new Date("2021-01-31"),locale);
r |=test(23,"2021-02-01",-2,new Date("2021-01-29"),locale);
r |=test(24,"2021-02-01",-3,new Date("2021-01-28"),locale);
r |=test(25,"2021-02-01",-4,new Date("2021-01-27"),locale);
r |=test(26,"2021-02-01",-365,new Date("2019-12-03"),locale);
r |=test(27,"2021-02-01",-100,new Date("2020-10-07"),locale);
r |=test(28,"2021-02-01",-999,new Date("2017-11-23"),locale);
if (r==0) console.log("✅ (Saturday only) Weekend Passed.");
//=====================================
// Tester Function
//=====================================
function test(test,startDate,wDays,should,locale) {
let result = addWorkDays(startDate,wDays,locale);
if (""+result !== ""+should) {
console.log(`${test}. Output : ${result}\n Should be: ${should}`);return 1;
}
}
this.setDate(this.getDate() + daysToAdd);
, I usedreturn this.addDays(daysToAdd);
because it more closely resembles the way the nativeDate.prototype.addDays
works. – Barbur