Long hours getting to work over time JS

Originally I intended to be looking for wheels, but can not find, a large majority are the problem, so I wrote myself a

Statement of needs

Support optional time period, start time and end time that is
determined based on the user's work and working hours
the number of hours return

Technology stack

moment.js

Thought process

How should I say, in fact, this logic is not very complicated
largely determine the start time, end time and clock on time, punch out the relationship between the time of
the start time <working hours ---> will begindate set working hours
start time> working hours ---> begindate the work time and date set to the +1
end time> after hours ---> enddate arranged to commute time
end time <business hours ---> begindate the work time and date set to -1

Then that judgment is not a working day
starting on the same day whether
probably thinking is so ah

/**
   * 
   * @param {*} st start 2018-02-02 12:00
   * @param {*} et end  2018-02-02 12:00
   * @param {*} das 打卡上班  10:00:00  格式必须
   * @param {*} dax 打卡下班  10:00:00  格式必须
   */
    GetWorkHours( st, et, das, dax) {
        var  das = {h:das.substr(0,2),m:das.substr(3,2),s:das.substr(6,2)}
        var  dax = {h:dax.substr(0,2),m:dax.substr(3,2),s:dax.substr(6,2)}
        var _totalHour = 0;
        //获取开始时间和结束时间
        var _beginDate = moment(st);
        var _endDate = moment(et);
        var _begin = {y:_beginDate.year(),M:_beginDate.month(),d:_beginDate.date()}
        var _end = {y:_endDate.year(),M:_endDate.month(),d:_endDate.date()}
        //整理
        if (_beginDate.isBefore(moment(Object.assign({},_begin,das)))) {
            //开始时间小于st,设置为st
            _beginDate.hour(das.h).minute(das.m);
        } else if (_beginDate.isAfter(moment(Object.assign({},_begin,dax)))) {
            _beginDate.add(1, 'd').hour(das.h).minute(das.m);
        }
        if (_endDate.isAfter(moment(Object.assign({},_end,dax)))) {
            //结束时间大于et,设置为et
            _endDate.hour(dax.h).minute(dax.m);
        } else if (_endDate.isBefore(moment(Object.assign({},_begin,das)))) {
            _beginDate.add(-1, 'd').hour(das.h).minute(das.m);
        }
        var _DateTime = _beginDate;
        while (moment(_DateTime).isSameOrBefore(_endDate, "day")) {
            //判断是否周日,周六
            var _week = moment(_DateTime).weekday();
            if (_week == 0 || _week == 6) {
                _DateTime.add(1, 'd');
                continue;
            }
            if (moment(_DateTime).isSame(_beginDate, "day")) {
                if (moment(_DateTime).isSame(_endDate, "day")) {
                    //开始时间和结束时间是同一天,结束时间-开始时间
                    _totalHour += _endDate.diff(_beginDate, "hours", true);
                    _DateTime.add(1, 'd');
                    continue;
                } else {
                    //开始时间和结束时间不是同一天,et-st
                    _totalHour += moment(dax).diff(das, "hours", true);
                    _DateTime.add(1, 'd');
                    continue;
                }
            } else if (moment(_DateTime).isSame(_endDate, "day")) {
                //是否和结束时间是同一天,结束时间-开始时间st,
                _totalHour += _endDate.diff(moment(_endDate).hour(das.h).minute(das.m), "hours", true);
                _DateTime.add(1, 'd');
                continue;
            } else {
                //工时
                _totalHour += moment.duration(moment(dax)-moment(das)).as('hours');
                _DateTime.add(1, 'd');
                continue;
            }
        }
        console.log("总计", _totalHour)
        return _totalHour 
    }

There may be flaws at the details, welcomed the discussion

Guess you like

Origin www.cnblogs.com/baimeishaoxia/p/11875332.html