如何使用moment.js计算JavaScript中两个日期之间的工作日数。我有一个计算这些天的工作公式,但公式不满足所有条件:
这是我的代码:
var start= moment(data[x].start_date);
var end= moment(data[x].est_end_date);
var difference= end.diff(start, 'days');
var workingDays= Math.round((difference/7)*5);
//data[x] is for iterating over a loop
我在这里每7天获得5天,因为星期六和星期日被视为非工作日,但如果从星期日或星期六开始计算天数,这个公式就会失败。
在这方面,任何人都可以帮助必须做出必要的改变以避免这个问题。
答案 0 :(得分:19)
将它分为3个部分..第一周,上周和中间部分,如下所示:
function workday_count(start,end) {
var first = start.clone().endOf('week'); // end of first week
var last = end.clone().startOf('week'); // start of last week
var days = last.diff(first,'days') * 5 / 7; // this will always multiply of 7
var wfirst = first.day() - start.day(); // check first week
if(start.day() == 0) --wfirst; // -1 if start with sunday
var wlast = end.day() - last.day(); // check last week
if(end.day() == 6) --wlast; // -1 if end with saturday
return wfirst + days + wlast; // get the total
}
测试代码
var ftest = {date:'2015-02-0',start:1,end:7};
var ltest = {date:'2015-02-2',start:2,end:8};
var f = 'YYYY-MM-DD';
for(var z=ftest.start; z<=ftest.end; ++z) {
var start = moment(ftest.date + z);
for(var y=ltest.start; y<=ltest.end; ++y) {
var end = moment(ltest.date + y);
var wd = workday_count(start,end);
console.log('from: '+start.format(f),'to: '+end.format(f),'is '+wd+' workday(s)');
}
}
输出测试代码:
from: 2015-02-01 to: 2015-02-22 is 15 workday(s)
from: 2015-02-01 to: 2015-02-23 is 16 workday(s)
from: 2015-02-01 to: 2015-02-24 is 17 workday(s)
from: 2015-02-01 to: 2015-02-25 is 18 workday(s)
from: 2015-02-01 to: 2015-02-26 is 19 workday(s)
from: 2015-02-01 to: 2015-02-27 is 20 workday(s)
from: 2015-02-01 to: 2015-02-28 is 20 workday(s)
from: 2015-02-02 to: 2015-02-22 is 15 workday(s)
from: 2015-02-02 to: 2015-02-23 is 16 workday(s)
from: 2015-02-02 to: 2015-02-24 is 17 workday(s)
from: 2015-02-02 to: 2015-02-25 is 18 workday(s)
from: 2015-02-02 to: 2015-02-26 is 19 workday(s)
from: 2015-02-02 to: 2015-02-27 is 20 workday(s)
from: 2015-02-02 to: 2015-02-28 is 20 workday(s)
from: 2015-02-03 to: 2015-02-22 is 14 workday(s)
from: 2015-02-03 to: 2015-02-23 is 15 workday(s)
from: 2015-02-03 to: 2015-02-24 is 16 workday(s)
from: 2015-02-03 to: 2015-02-25 is 17 workday(s)
from: 2015-02-03 to: 2015-02-26 is 18 workday(s)
from: 2015-02-03 to: 2015-02-27 is 19 workday(s)
from: 2015-02-03 to: 2015-02-28 is 19 workday(s)
from: 2015-02-04 to: 2015-02-22 is 13 workday(s)
from: 2015-02-04 to: 2015-02-23 is 14 workday(s)
from: 2015-02-04 to: 2015-02-24 is 15 workday(s)
from: 2015-02-04 to: 2015-02-25 is 16 workday(s)
from: 2015-02-04 to: 2015-02-26 is 17 workday(s)
from: 2015-02-04 to: 2015-02-27 is 18 workday(s)
from: 2015-02-04 to: 2015-02-28 is 18 workday(s)
from: 2015-02-05 to: 2015-02-22 is 12 workday(s)
from: 2015-02-05 to: 2015-02-23 is 13 workday(s)
from: 2015-02-05 to: 2015-02-24 is 14 workday(s)
from: 2015-02-05 to: 2015-02-25 is 15 workday(s)
from: 2015-02-05 to: 2015-02-26 is 16 workday(s)
from: 2015-02-05 to: 2015-02-27 is 17 workday(s)
from: 2015-02-05 to: 2015-02-28 is 17 workday(s)
from: 2015-02-06 to: 2015-02-22 is 11 workday(s)
from: 2015-02-06 to: 2015-02-23 is 12 workday(s)
from: 2015-02-06 to: 2015-02-24 is 13 workday(s)
from: 2015-02-06 to: 2015-02-25 is 14 workday(s)
from: 2015-02-06 to: 2015-02-26 is 15 workday(s)
from: 2015-02-06 to: 2015-02-27 is 16 workday(s)
from: 2015-02-06 to: 2015-02-28 is 16 workday(s)
from: 2015-02-07 to: 2015-02-22 is 10 workday(s)
from: 2015-02-07 to: 2015-02-23 is 11 workday(s)
from: 2015-02-07 to: 2015-02-24 is 12 workday(s)
from: 2015-02-07 to: 2015-02-25 is 13 workday(s)
from: 2015-02-07 to: 2015-02-26 is 14 workday(s)
from: 2015-02-07 to: 2015-02-27 is 15 workday(s)
from: 2015-02-07 to: 2015-02-28 is 15 workday(s)
答案 1 :(得分:6)
我使用一个简单的功能来实现这一目标。也许不是最有效但它有效。它不需要Moment.js。它只是Javascript。
function getNumWorkDays(startDate, endDate) {
var numWorkDays = 0;
var currentDate = new Date(startDate);
while (currentDate <= endDate) {
// Skips Sunday and Saturday
if (currentDate.getDay() !== 0 && currentDate.getDay() !== 6) {
numWorkDays++;
}
currentDate = currentDate.addDays(1);
}
return numWorkDays;
}
到addDays
,我使用以下函数:
Date.prototype.addDays = function (days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
答案 2 :(得分:3)
我发现kokizzu的答案如果在同一周(例如太阳坐下)的日子里没有工作,那么就这样解决了:
function calcBusinessDays(startDate, endDate) {
var day = moment(startDate);
var businessDays = 0;
while (day.isSameOrBefore(endDate,'day')) {
if (day.day()!=0 && day.day()!=6) businessDays++;
day.add(1,'d');
}
return businessDays;
}
答案 3 :(得分:2)
我已经改编了Kokizzu的答案,以解决夏令时问题。在巴西时区(格林威治标准时间-3),2017年10月17日至2017年10月18日之间的差异为-2.71而不是2。
本周开始时间为2017年5月15日00:00 UTC或14/10/2017 21:00-03:00
本周末是22/10/2017 00:00 UTC或21/10/2017 22:00-02:00(夏令时)
因此,而不是7,&#34;第一&#34;之间的差异。和&#34;最后&#34;天数变量为6(或8,取决于您的时区)。
修复的代码如下:
start = moment(start).utc().add(start.utcOffset(), 'm'); // Ignore timezones
end = moment(end).utc().add(end.utcOffset(), 'm'); // Ignore timezones
var first = start.clone().endOf('week'); // end of first week
var last = end.clone().startOf('week'); // start of last week
// Fixing Summer Time problems
firstCorrection = moment(first).utc().add(60, 'm').toDate(); //
var days = last.diff(firstCorrection,'days') * 5 / 7; // this will always multiply of 7
var wfirst = first.day() - start.day(); // check first week
if(start.day() == 0) --wfirst; // -1 if start with sunday
var wlast = end.day() - last.day(); // check last week
if(end.day() == 6) --wlast; // -1 if end with saturday
return wfirst + days + wlast; // get the total (subtract holidays if needed)
答案 4 :(得分:2)
通过使用momentjs,可能是这样的:
private calculateWorkdays(startDate: Date, endDate: Date): number {
// + 1 cause diff returns the difference between two moments, in this case the day itself should be included.
const totalDays: number = moment(endDate).diff(moment(startDate), 'days') + 1;
const dayOfWeek = moment(startDate).isoWeekday();
let totalWorkdays = 0;
for (let i = dayOfWeek; i < totalDays + dayOfWeek; i++) {
if (i % 7 !== 6 && i % 7 !== 0) {
totalWorkdays++;
}
}
return totalWorkdays;
}
答案 5 :(得分:0)
您可以尝试此操作(无循环):
function getBusinessDays(endDate, startDate) {
var lastDay = moment(endDate);
var firstDay = moment(startDate);
let calcBusinessDays = 1 + (lastDay.diff(firstDay, 'days') * 5 -
(firstDay.day() - lastDay.day()) * 2) / 7;
if (lastDay.day() == 6) calcBusinessDays--;//SAT
if (firstDay.day() == 0) calcBusinessDays--;//SUN
return calcBusinessDays;
}
答案 6 :(得分:0)
我已经使用了有用的库 moment-business-days 以及 moment
NPM Link of Moment-business-days
const diff = moment('05-15-2017', 'MM-DD-YYYY').businessDiff(moment('05-08-2017','MM-DD-YYYY'));
// diff = 5