Изменение дат, чтобы они были в рабочие часы - PullRequest
0 голосов
/ 17 октября 2018

На шаге JavaScript в Pentaho Data Integration я хочу вычислить время в часах, которое проходит между одной датой и другой.

После ознакомления с этим этим сообщением в блоге я понимаю, что мне нужно настроить значения startDate и endDate в функции ниже, которые выходят за пределы рабочего времени, чтобы они были в рабочее времяпоэтому функция не возвращает ноль.Даты в формате 27.09.2008 18:54:55.

Вот моя попытка:

var Approve_Gap;
var created_at_copy;
var approved_at_copy1;


// Function that accepts two parameters and calculates
// the number of hours worked within that range
function workingHoursBetweenDates(startDate, endDate) {  
    // Store minutes worked
    var minutesWorked = 0;

    // Validate input
    if (endDate < startDate) { return 0; }

    // Loop from your Start to End dates (by hour)
    var current = startDate;

    // Define work range
    var workHoursStart = 8;
    var workHoursEnd = 17;
    var includeWeekends = true;

    // bring dates into business hours

    if(current.getHours() > workHoursEnd) {
        current = current - (current.getHours() - workHoursEnd);
    }

    else if(current.getHours() < workHoursStart) {
        current = current + (workHoursStart - current.getHours()) 
    }

    if(endDate.getHours() > workHoursEnd) {
        endDate = endDate - (endDate.getHours() - workHoursEnd);
    }

    else if(endDate.getHours() < workHoursStart) {
        endDate = endDate + (workHoursStart - endDate.getHours()) 
    }

    // Loop while currentDate is less than end Date (by minutes)
    while(current <= endDate){   

        // Is the current time within a work day (and if it 
        // occurs on a weekend or not)          
        if(current.getHours() >= workHoursStart && current.getHours() < workHoursEnd && (includeWeekends ? current.getDay() !== 0 && current.getDay() !== 6 : true)){
              minutesWorked++;
        }

        // Increment current time
        current.setTime(current.getTime() + 1000 * 60);
    }

    // Return the number of hours
    return minutesWorked / 60;

}


Approve_Gap = workingHoursBetweenDates(created_at_copy, approved_at_copy1);

1 Ответ

0 голосов
/ 19 октября 2018

Я получил даты в рабочие часы, настроив копии дат, как показано ниже:

if(created_at_copy.getHours() >= workHoursEnd) {
    created_at_copy.setDate(created_at_copy.getDate() + 1);
    created_at_copy.setHours(8);
    created_at_copy.setMinutes(0);
    created_at_copy.setSeconds(0);

} else if(created_at_copy.getHours() < workHoursStart) {
    created_at_copy.setHours(8);
    created_at_copy.setMinutes(0);
    created_at_copy.setSeconds(0);

}

if(approved_at_copy1.getHours() >= (workHoursEnd)) {
    approved_at_copy1.setDate(approved_at_copy1.getDate() + 1);
    approved_at_copy1.setHours(8);
    approved_at_copy1.setMinutes(0);
    created_at_copy.setSeconds(0);

} else if(approved_at_copy1.getHours() < workHoursStart) {
    approved_at_copy1.setHours(8);
    approved_at_copy1.setMinutes(0);
    created_at_copy.setSeconds(0);
}
...