vue-计算请假时间(除去午休与节假日)
/*** 计算请假时长* @param {Object} beginTime开始时间* @param {Object} endTime结束时间* @param {Object} stWorkTime上班时间* @param {Object} enWrokTime下班时间* @param {Object} isFreeTime是否要去除午休工作时长* @param {Object} freeTime
·
/**
* 计算请假时长
* @param {Object} beginTime 开始时间
* @param {Object} endTime 结束时间
* @param {Object} stWorkTime 上班时间
* @param {Object} enWrokTime 下班时间
* @param {Object} isFreeTime 是否要去除午休工作时长
* @param {Object} freeTimeMon 午休开始时间
* @param {Object} freeTimeAft 午休结束时间
* @param { Array } fillterDatas 节假日日期
* @param { String } dateType 请假类型 如果为产假陪产假,不排除节假日与周末
*
*/
import Vue from 'vue'
import { formatTimestamp } from "./moment"
export function getLeave(beginTime, endTime, stWorkTime, enWrokTime, isFreeTime, freeTimeMon, freeTimeAft, fillterDatas, dateType) {
var days;
var hours;
var date;
var freeTime = freeTimeAft - freeTimeMon;
// beginTime = beginTime.replace(/-/g, '/');
var beginArr = beginTime.split(" ");
var beginMonth = parseInt(beginArr[0].split("-")[1]);
var beginDay = parseInt(beginArr[0].split("-")[2]);
var beginHours = parseInt(beginArr[1].split(":")[0]);
var beginMin = parseInt(beginArr[1].split(":")[1]);
var beginHoursMin = beginHours + beginMin / 60;
// endTime = endTime.replace(/-/g, '/');
var endArr = endTime.split(" ");
var endMonth = parseInt(endArr[0].split("-")[1]);
var endDay = parseInt(endArr[0].split("-")[2]);
var endHours = parseInt(endArr[1].split(":")[0]);
var endMin = parseInt(endArr[1].split(":")[1]);
var endHoursMin = endHours + endMin / 60;
//如果beginHoursMin时间小于上班时间都算上班时间
if(beginHoursMin <= stWorkTime) {
beginHoursMin = stWorkTime;
}
//如果endHoursMin时间大于上班时间都算下班时间
if(endHoursMin >= enWrokTime) {
endHoursMin = enWrokTime;
}
//如果endHoursMin时间小于上班时间都算下班时间
if(endHoursMin <= stWorkTime) {
endHoursMin = stWorkTime;
}
//如果结束时间在freeTimeMon和freeTimeAft之间都算freeTimeMon
if(isFreeTime == true) {
if(endHoursMin >= freeTimeMon && endHoursMin <= freeTimeAft) {
endHoursMin = freeTimeMon;
}
}
//获取结束时间-开始时间的天数
var daysBetweenlist = daysBetween(beginTime, endTime);
let effectiveLeaveDate
console.log("无效的请假日期数组", daysBetweenlist)
if(dateType == 5 || dateType == 7 || dateType == 8 || dateType == 9 ) {
effectiveLeaveDate = daysBetweenlist;
} else {
effectiveLeaveDate = daysBetweenlist.filter(date => !fillterDatas.includes(date)).filter(date => (new Date(date).getDay() != 6 && new Date(date).getDay() != 0))
}
console.log("有效的请假日期数组【过滤节假日期后的】", effectiveLeaveDate)
if(beginTime > endTime) {
Vue.prototype.$Message.warning({
content: '开始时间需小于结束时间',
duration: 3
});
return false;
}
console.log(beginTime.split(' ')[0])
if(!effectiveLeaveDate.includes(beginTime.split(' ')[0]) || !effectiveLeaveDate.includes(endTime.split(' ')[0])) {
Vue.prototype.$Message.warning({
content: '调休开始、结束时间需在工作日,请您重新选择',
duration: 3
});
return false;
}
if(effectiveLeaveDate.length > 0) {
var daysBetweenLen = effectiveLeaveDate.length;
//午休
if(isFreeTime == true) {
var hour = enWrokTime - stWorkTime - freeTime;
if(daysBetweenLen == 1) {
//同一天
if(endHoursMin - freeTimeAft > 0 && beginHoursMin <= freeTimeMon) {
hours = (endHoursMin) - (beginHoursMin) - freeTime;
} else {
hours = (endHoursMin) - (beginHoursMin)
}
} else if(daysBetweenLen == 2) {
//跨一天
//第一天的时长
hours = enWrokTime - beginHoursMin;
//是否有午休
if(beginHoursMin <= freeTimeMon)
hours = hours - freeTime;
//第二天的时长
hours += endHoursMin - stWorkTime;
//是否有午休
if(endHoursMin >= freeTimeAft)
hours = hours - freeTime;
} else {
//跨两天以上
//第一天的时长
hours = enWrokTime - beginHoursMin;
//是否有午休
if(beginHoursMin <= freeTimeMon)
hours = hours - freeTime;
//中间时长
hours += (daysBetweenLen - 2) * (hour);
//最后一天时长
hours += endHoursMin - stWorkTime;
//是否有午休
if(endHoursMin >= freeTimeAft)
hours = hours - freeTime;
}
days = Math.floor(hours / hour);
hours = hours % hour;
date = {
"days": days,
"hours": hours
};
} else {
//非午休
var hour = enWrokTime - stWorkTime;
if(daysBetweenLen == 1) {
//同一天
hours = (endHoursMin) - (beginHoursMin);
} else if(daysBetweenLen == 2) {
//跨一天
hours = enWrokTime - beginHoursMin;
//第二天的时长
hours += endHoursMin - stWorkTime;
} else {
//跨两天以上
//第一天的时长
hours = enWrokTime - beginHoursMin;
//中间时长
hours += (daysBetweenLen - 2) * (hour);
//最后一天时长
hours += endHoursMin - stWorkTime;
}
days = Math.floor(hours / hour);
hours = hours % hour;
date = {
"days": days,
"hours": hours
};
}
}
return date;
}
/**
* 根据两个日期,判断相差天数
* @param bdate 开始日期 如:2016-11-01
* @param edate 结束日期 如:2016-11-02
* @returns {number} 返回相差天数
*/
export function daysBetween(bdate, edate) {
let oneDayMillisecond = 86400000 //一天毫秒数
let bdateMillisecond = new Date(bdate).getTime()
let edateMillisecond = new Date(edate).getTime()
let diffMillisecond = edateMillisecond - bdateMillisecond
let diffDays = Math.floor(Math.abs(diffMillisecond / oneDayMillisecond))
let date_arr = []
for (let index = 0; index <= diffDays; index++) {
let new_date = formatTimestamp(bdateMillisecond + oneDayMillisecond*index, "YYYY-MM-DD")
date_arr.push(new_date)
}
// console.log('生成两个起止日期之间的日期数组',date_arr)
return date_arr
}
export function gDate(datestr) {
datestr = datestr.replace(/-/g, '/');
var temp = datestr.split("/");
var date = new Date(temp[0], temp[1], temp[2]);
return date;
}
/**
* 计算请假时长
* @param {Object} beginTime 开始时间
* @param {Object} endTime 结束时间
* @param {Object} stWorkTime 上班时间
* @param {Object} enWrokTime 下班时间
* @param {Object} isFreeTime 是否要去除午休工作时长
* @param {Object} freeTimeMon 午休开始时间
* @param {Object} freeTimeAft 午休结束时间
* @param { String } dateType 请假类型 如果为产假陪产假,不排除节假日与周末
*
*
*/
//获取请假时间-wjq
上文中所用的方法
/**
* 格式化时间戳
* @param {时间戳,毫秒} timestamp
* @param {格式} format
*/
export function formatTimestamp(timestamp, format='YYYY-MM-DD HH:mm') {
return moment(timestamp).format(format);
}
注:。
我的代码参考来自:https://www.jianshu.com/p/91be21fd4ea7
更多推荐
已为社区贡献1条内容
所有评论(0)