I have the following problem:
- Choose a start date.
- Enter the number of working hours
- Returns the actual working day (excluding Saturday and Sunday).
Conditions:
- 1 working week 5 days (Monday to Friday)
- An 8 hour working day
Here's my implementation so far, but somehow it only works for even weeks:
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));
})
How can I calculate the end date that matches the conditions listed?