const workTime = function (start, end) {
start = new Date(start);
end = new Date(end);
const at = 8.5,
bt = 1.5,
ct = 6,
dt = 16,
startDate = start.toLocaleDateString(), // 日期
endDate = end.toLocaleDateString();
let res = (end - start) / 1000 / 3600;
// 同一天
if (startDate === endDate) {
if (start.getHours() < 12 && end.getHours() > 12) {
res = res - bt;
}
}
else {
// 相差一天
res = res - at - ct;
if (start.getHours() < 12) {
res = res - bt;
}
if (end.getHours() > 12) {
res = res - bt;
}
// 超过一天
const cDate = (new Date(endDate) - new Date(startDate)) / 3600 / 24 / 1000;
if (cDate > 1) {
res = res - dt * (cDate - 1);
}
}
return res;
}
console.log(workTime('2017/03/15 9:30', '2017/03/16 16:30'));
// 8:30上班,下午1:30上班,6点下班