根据需求请假时间要排除法定节假日和非工作时间
节假日是每年更新的,没有固定接口,需要手动录入
个人根据官方的节假日整理了当年的所有节假日,可以根据个人需求进行修改
- // 获取每个月的节假日,如果当月没有节假日就默认星期六星期天
- holidays: [
- [],// 1月
- [3, 10, 11, 12, 13, 14, 15, 16, 17, 24, 25],// 2月
- [],// 3月
- [4, 5, 6, 13, 14, 20, 21, 27],// 4月
- [1, 2, 3, 4, 5, 12, 18, 19, 25, 26],// 5月
- [1, 2, 8, 9, 10, 15, 16, 22, 23, 29, 30],// 6月
- [],// 7月
- [],// 8月
- [7, 8, 15, 16, 17, 21, 22, 28],// 9月
- [1, 2, 3, 4, 5, 6, 7, 13, 19, 20, 26, 27],// 10月
- [],// 11月
- [],// 12月
- ]
这里是封装的计算方法,传入开始时间和结束时间 时间格式为:年-月-日 时:分
里面定义了上班开始结束时间和中午休息时间,可以自定义
- // 计算工作时间调休小时数
- calculateLeaveTime(startTime, endTime) {
- // console.log(startTime);
- // console.log(endTime);
- // 工作开始结束时间
- const workStart = 9;
- const workEnd = 18;
- // 休息开始结束时间
- const restStart = 12;
- const restEnd = 13;
- // 请假天数
- let day = 0;
- // 总计小时数
- let total_hour = 0;
-
- // 循环每天
- for (let date = new Date(startTime); date <= new Date(endTime); date.setDate(date.getDate() + 1)) {
- day++
- // 每天多少小时
- let dayLeaveTime = 0;
- let m = date.getMonth()
- // 获取当月是否有节假日 我存储到vuex里面的,通过下标获取当月的节假日
- let holiday_arr = this.$store.state.holidays[m]
- console.log(holiday_arr);
- // 获取今天是星期几
- let x = date.getDay();
- // 今天是几号
- let i = date.getDate();
- // console.log(i + "号");
- // console.log("星期" + x);
- // 有时候要补节假日获取手动录入的节假日进行计算
- if (holiday_arr.length != 0 && holiday_arr.includes(i)) {
- // console.log(i + "号");
- continue;
- }
- // 除开中午休息时间和星期六星期天
- if ([0, 6].includes(x) && holiday_arr.length == 0) {
- // console.log("星期" + x);
- continue;
- }
- // 上面判断是否计算当前
- // 第一天要获取开始时间和结束时间
- if (day == 1) {
- // 小时
- let h = 0
- let h1 = startTime.split(" ")[1].split(":")[0] * 1
- let h2 = endTime.split(" ")[1].split(":")[0] * 1
- // 分
- let m1 = startTime.split(" ")[1].split(":")[1] * 1
- let m2 = endTime.split(" ")[1].split(":")[1] * 1
- // 判断结束时间是否大于开始时间
- if (h2 >= h1) {
- // 判断上午还是下午还是跨了中午
- if (h1 < restEnd && h2 >= restStart) {
- // 跨了中午 是否在休息时间内
- if (h1 >= restStart) {
- h1 = restStart
- m1 = 0
- }
- if (h2 < restEnd) {
- h2 = restEnd
- m2 = 0
- }
- // console.log("跨了中午", restStart - h1 + h2 - restEnd);
- h = restStart - h1 + h2 - restEnd
- } else if (h1 <= restStart && h2 <= restStart) {
- // 上午
- // console.log("上午", h2 - h1);
- h = h2 - h1
- } else if (h1 >= restEnd && h2 >= restEnd) {
- // 下午
- // console.log("下午", h2 - h1);
- h = h2 - h1
- }
- } else {
- // 判断上午还是下午还是跨了中午
- if (h1 >= restEnd && h2 < restEnd) {
- // 跨了中午
- if (h2 >= restStart) {
- h2 = restStart
- m2 = 0
- }
- // console.log("跨了中午", workEnd - h1 + h2 - workStart);
- h = workEnd - h1 + h2 - workStart
- } else if (h1 <= restStart && h2 <= restStart) {
- // 上午
- // console.log("上午", workEnd - h1 + h2 - workStart);
- h = workEnd - h1 + h2 - workStart - (restEnd - restStart)
- } else if (h1 >= restEnd && h2 >= restEnd) {
- // 下午
- // console.log("下午", restStart - workStart + (workEnd - h1) + (h2 - restEnd));
- h = restStart - workStart + (workEnd - h1) + (h2 - restEnd)
- }
- }
- // 计算分钟
- // console.log("第一天的小时", h);
- // console.log("第一天的分钟", (m2 - m1) / 60);
- dayLeaveTime = h + ((m2 - m1) / 60)
- if (h < 0 || (h <= 0 && ((m2 - m1) / 60) <= 0)) {
- // "结束时间必须大于开始时间"
- this.tips = true;
- dayLeaveTime = 0;
- } else {
- this.tips = false;
- }
- } else {
- // 第二天开始 循环每天的工作时间范围的小时数
- for (let hour = workStart; hour < workEnd; hour++) {
- if (hour <= restStart || hour > restEnd) {
- dayLeaveTime += 1
- }
- }
- }
- // console.log(date.getDate() + "号的小时数", dayLeaveTime);
- total_hour += dayLeaveTime
- }
- // console.log(day + "天");
- // console.log(total_hour + "小时");
- return total_hour + "小时";
- },
这里按每天八小时计算,排除了2024年法定节假日 劳动节的调休 一共使用了工作时间的32小时
- // 计算工作日时间进度
- // 获取当前时间
- const now = new Date();
- // 获取当前年份和月份
- const currentYear = now.getFullYear();
- const currentMonth = now.getMonth();
- // 获取vuex里面存储的节假日
- let holidays = this.$store.state.holidays[currentMonth]
- // console.log("当月节假日", holidays);
- // 计算当月天数
- const daysInMonth = new Date(currentYear, currentMonth + 1, 0).getDate();
- // 当月几号
- const dayOfMonth = now.getDate();
- // console.log("当月天数:", daysInMonth);
- // console.log("当月的第", dayOfMonth, "天");
- // 工作日天数
- let workday = 0;
- // 当前工作日天数
- let Month = 0;
- // 当前时间进度
- let num = "0%";
- // 判断是否设置节假日
- if (holidays.length) {
- // 自定义节假日
- workday = daysInMonth - holidays.length;
- // console.log("自定义工作日", workday);
- // 默认已工作日
- for (let i = 1; i < dayOfMonth + 1; i++) {
- if (!holidays.includes(i)) {
- Month++;
- }
- }
- num = Month / workday;
- } else {
- // 循环默认天数
- for (let i = 1; i < daysInMonth + 1; i++) {
- let date = new Date(
- new Date().getFullYear(),
- new Date().getMonth(),
- i
- );
- // 遍历每天获取星期几
- let x = date.getDay();
- // 不是节假日工作日就加一
- if (![0, 6].includes(x)) {
- workday++;
- }
- }
- // console.log("默认工作日", workday);
- // 默认已工作日
- for (let i = 1; i < dayOfMonth + 1; i++) {
- let date = new Date(
- new Date().getFullYear(),
- new Date().getMonth(),
- i
- );
- // 遍历每天获取星期几
- let x = date.getDay();
- // 不是节假日工作日就加一
- if (![0, 6].includes(x)) {
- Month++;
- }
- }
- num = Month / workday;
- }
- // console.log("已工作", Month, "天");
- // console.log("时间进度" + (num * 100).toFixed(1) + "%");
- // 赋值时间进度
- this.less_day = (num * 100).toFixed(1).replace(/\.0$/, "") + "%";