通过javascript计算日期时间



我有以下问题:

  1. 选择开始日期
  2. 输入工作小时数
  3. 返回实际工作日(不包括周六和周日(

条件:

  • 1个工作周5天(周一至周五(
  • 每天工作8小时

这是我到目前为止的实现,但不知何故,它只工作了几周:

function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
$('#startTimeProject,#doTimeProject').change(function() {
var startTime = $('#startTimeProject').val();
var doTime = $('#doTimeProject').val();
if (doTime == null) {
return;
}
const d = new Date(startTime);
var currentDay = d.getDay();
startTime = new Date(startTime);
if (currentDay == 0) {
startTime.setDate(startTime.getDate() + 1);
}
if (currentDay == 6) {
startTime.setDate(startTime.getDate() + 2);
}
startTime = Date.parse(startTime);
$('#startTimeProject').val(formatDate(startTime));
var numberWeek = Math.floor(doTime / 40);
if (numberWeek >= 1) {
doTime = Number(doTime) + numberWeek * 16;
}
var dayTime = Math.ceil(doTime / 8);
var endTime = new Date(startTime);
endTime.setDate(endTime.getDate() + dayTime);
endTime = Date.parse(endTime);
$('#endTimeProject').val(formatDate(endTime));
})

如何计算符合所列条件的结束日期?

我有答案

$('#startTimeProject,#doTimeProject').change(function () {
// start time
var startTime = $('#startTimeProject').val();
// do time
var doTime = $('#doTimeProject').val();
if (doTime == null) {
return;
}
const d = new Date(startTime);
var currentDay = d.getDay();

startTime = new Date(startTime);

// If the start date is Sunday, it will be considered as Monday
if (currentDay == 0) {
startTime.setDate(startTime.getDate() + 1);
}
// If the start date is Saturday, it will be considered as Monday
if (currentDay == 6) {
startTime.setDate(startTime.getDate() + 2);
}
startTime = Date.parse(startTime)

$('#startTimeProject').val(formatDate(startTime));

var numberWeek = Math.floor(doTime / 40);
// over 1 week, each time expires + 16h
var dayTime = Math.ceil(doTime / 8);// 1 day = 8 hours of work
const dNew = new Date(startTime);
var currentDayNew = dNew.getDay();
if (numberWeek == 1) {
// doTime = Number(doTime) + numberWeek * 16;
// if = 1 week then + add 2 days
dayTime = dayTime + 2;
}
// if it is more than 1 week, it will still be calculated the same, only + add 2 days of the previous week
else if (numberWeek > 1) {
changeTime = dayTime - (numberWeek * 5);

if (changeTime == 0) {
dayTime = Math.ceil(doTime / 8) + numberWeek * 2;
} else {
switch (currentDayNew) {

case 1:
dayTime = dayTime + numberWeek * 5;
break;
case 2:

if (changeTime == 4) {
dayTime = changeTime + 2 + numberWeek * 7;
} else {
dayTime = changeTime + numberWeek * 7;
}
break;
case 3:

if (changeTime == 4) {
dayTime = changeTime + 2 + numberWeek * 7;
}
else if (changeTime == 3) {
dayTime = changeTime + 2 + numberWeek * 7;
}
else {
dayTime = changeTime + numberWeek * 7;
}
break;
case 4:

if (changeTime == 4) {
dayTime = changeTime + 2 + numberWeek * 7;
}
else if (changeTime == 3) {
dayTime = changeTime + 2 + numberWeek * 7;
}
else if (changeTime == 2) {
dayTime = changeTime + 2 + numberWeek * 7;
}
else {
dayTime = changeTime + numberWeek * 7;
}
break;
case 5:

dayTime = changeTime + 2 + numberWeek * 7;
break;
}
}
}
else {
//less than 1 week => dayTime will be less than 5
switch (currentDayNew) {
// monday won't increase anything
case 1:
break;
case 2:
// tuesday, daytime =  4 then bonus 2 days
if (dayTime == 4) {
dayTime = dayTime + 2
}
break;
case 3:
// Wednesday, daytime =  4,daytime =  3 then bonus 2 days
if (dayTime == 4) {
dayTime = dayTime + 2
}
if (dayTime == 3) {
dayTime = dayTime + 2
}
break;
case 4:
// Thusday, daytime =  4,daytime =  3,daytime =  2 then bonus 2 days
if (dayTime == 4) {
dayTime = dayTime + 2
}
if (dayTime == 3) {
dayTime = dayTime + 2
}
if (dayTime == 2) {
dayTime = dayTime + 2
}
break;
case 5:
// Friday bonus 2 days
dayTime = dayTime + 2
break;
}
} 
var endTime = new Date(startTime);
endTime.setDate(endTime.getDate() + dayTime);
endTime = Date.parse(endTime)
$('#endTimeProject').val(formatDate(endTime));
})

相关内容

  • 没有找到相关文章

最新更新