想试着做一个请假表,第一步就难住了。时长怎么计算?- 字段有start_day、end_day、start_part(上午、下午)、end_part(上午、下午),例如start_day=2024.11.14,start_part=上午,end_day=2024.11.14,end_part=上午,则值为0.5。start_day=2024.11.14,start_part=上午,end_day=2024.11.14,end_part=下午,则值为1。start_day=2024.11.14,start_part=上午,end_day=2024.11.15,end_part=上午,则值为1.5。start_day=2024.11.14,start_part=上午,end_day=2024.11.15,end_part=下午,则值为2。start_day=2024.11.14,start_part=下午,end_day=2024.11.15,end_part=上午,则值为1。start_day=2024.11.14,start_part=下午,end_day=2024.11.15,end_part=下午,则值为1.5。
复制代码 不知道这个公式该怎么写,然后还要考虑周末的情况。在网上找了一个代码,但不知道怎么用。- /**
- * 计算小时
- * bDate 开始时间
- * eDate 结束时间
- */
- countHours(start_time_date, start_time_timer, end_time_date, end_time_timer) {
- let bDateDayY = new Date(start_time_date)//开始时间
- let bDateDayT = start_time_timer; // 上午、下午
- let eDateDayY = new Date(end_time_date)//结束时间
- let eDateDayT = end_time_timer;
- let bisweek = bDateDayY.getDay(); //获取开始时间是周几
- let eisweek = eDateDayY.getDay(); //获取结束时间是周几
- if ((bisweek != 0 && bisweek != 6) && (eisweek != 0 && eisweek != 6)) { //开始时间和结束时间都不是周末
- var mosconds = eDateDayY.getTime() - bDateDayY.getTime() //时间 差的 毫秒数
- let days = (eDateDayY - bDateDayY) / 1000 / 3600 / 24; //计算出相差天数
- let weeks = this.checkWeekend(bDateDayY, eDateDayY, days); //周末天数
- if (weeks > 0) { //有周末
- days = days - weeks; //去掉周末天数
- if ((bDateDayT == '下午' && eDateDayT == '上午')) { //开始时间是下午,结束时间是上午
- days = days - 0.5; //去掉半天
- }
- if ((bDateDayT == '下午' && eDateDayT == '下午')) { //开始时间是下午,结束时间是下午
- days = days; //不用去掉半天
- }
- if ((bDateDayT == '上午' && eDateDayT == '下午')) { //开始时间是上午,结束时间是下午
- days = days + 1; //加一天
- }
- if ((bDateDayT == '上午' && eDateDayT == '上午')) { //开始时间是上午,结束时间是上午
- days = days + 0.5; //加半天
- }
- return days; //返回天数
- } else { //没有周末
- if (mosconds == 0) { //同天
- if ((bDateDayT == '上午' && eDateDayT == '下午')) { //开始时间是上午,结束时间是下午
- days = days + 1; //加一天
- }
- if (eDateDayT == '上午') { //结束时间是上午
- days = days + 0.5; //加半天
- }
- return days;
- } else {
- if ((bDateDayT == '上午' && eDateDayT == '下午')) {
- days = days + 1;
- }
- if ((bDateDayT == '上午' && eDateDayT == '上午')) {
- days = days + 0.5;
- }
- if ((bDateDayT == '下午' && eDateDayT == '上午')) {
- days = days + 0.5;
- }
- if ((bDateDayT == '下午' && eDateDayT == '下午')) {
- days = days + 0.5;
- }
- return days;
- }
- }
- } else if ((bisweek == 0 || bisweek == 6) || (eisweek == 0 || eisweek == 6)) { //开始时间或结束时间是周末
- if ((bisweek == 6 && eisweek == 0) || (bisweek == 6 && eisweek == 6) || (bisweek == 0 && eisweek == 0)) { //开始时间是周六或周日,结束时间是周日或周六
- // wx.showModal({
- // title: '提示',
- // content: '这段时间休息不需要请假!',
- // })
- let days = 0;
- return days;
- } else { //开始时间是周末,结束时间是工作日
- if (bisweek == 0 || bisweek == 6) {
- var mosconds = eDateDayY.getTime() - bDateDayY.getTime() //时间 差的 毫秒数
- let days = (eDateDayY - bDateDayY) / 1000 / 3600 / 24; //计算出相差天数
- let weeks = this.checkWeekend(bDateDayY, eDateDayY, days); //周末天数
- if (weeks > 0) {
- if (days >= weeks) {
- days = days - weeks;
- if ((bDateDayT == '下午' && eDateDayT == '上午')) {
- days = days - 0.5;
- }
- if ((bDateDayT == '下午' && eDateDayT == '下午')) {
- days = days;
- }
- if ((bDateDayT == '上午' && eDateDayT == '下午')) {
- days = days + 1;
- }
- if ((bDateDayT == '上午' && eDateDayT == '上午')) {
- days = days + 0.5;
- }
- } else {
- days = 0
- }
- return days;
- } else { //没有周末
- if (mosconds == 0) { //同天
- if ((bDateDayT == '上午' && eDateDayT == '下午')) {
- days = days + 1;
- }
- if (eDateDayT == '上午') {
- days = days + 0.5;
- }
- return days;
- } else {
- if ((bDateDayT == '上午' && eDateDayT == '下午')) {
- days = days + 1;
- }
- if ((bDateDayT == '上午' && eDateDayT == '上午')) {
- days = days + 0.5;
- }
- if ((bDateDayT == '下午' && eDateDayT == '上午')) {
- days = days + 0.5;
- }
- if ((bDateDayT == '下午' && eDateDayT == '下午')) {
- days = days + 0.5;
- }
- return days;
- }
- }
- // wx.showModal({
- // title: '提示',
- // content: '开始日期不可为周末',
- // })
- }
- if (eisweek == 0 || eisweek == 6) {
- var mosconds = eDateDayY.getTime() - bDateDayY.getTime() //时间 差的 毫秒数
- let days = (eDateDayY - bDateDayY) / 1000 / 3600 / 24; //计算出相差天数
- let weeks = this.checkWeekend(bDateDayY, eDateDayY, days); //周末天数
- if (weeks > 0) {
- if (days >= weeks) {
- days = days - weeks;
- if ((bDateDayT == '下午' && eDateDayT == '上午')) {
- days = days + 0.5;
- }
- if ((bDateDayT == '下午' && eDateDayT == '下午')) {
- days = days + 0.5;
- }
- if ((bDateDayT == '上午' && eDateDayT == '下午')) {
- days = days;
- }
- if ((bDateDayT == '上午' && eDateDayT == '上午')) {
- days = days;
- }
- } else {
- days = 0
- }
- return days;
- } else {
- if (mosconds == 0) { //同天
- if ((bDateDayT == '上午' && eDateDayT == '下午')) {
- days = days + 1;
- }
- if ((bDateDayT == '下午' && eDateDayT == '下午')) {
- days = days + 0.5;
- }
- if ((bDateDayT == '上午' && eDateDayT == '上午')) {
- days = days + 0.5;
- }
- return days;
- } else {
- if ((bDateDayT == '上午' && eDateDayT == '下午')) {
- days = days + 1;
- }
- if ((bDateDayT == '上午' && eDateDayT == '上午')) {
- days = days + 0.5;
- }
- if ((bDateDayT == '下午' && eDateDayT == '上午')) {
- days = days + 0.5;
- }
- if ((bDateDayT == '下午' && eDateDayT == '下午')) {
- days = days + 0.5;
- }
- return days;
- }
- }
- // wx.showModal({
- // title: '提示',
- // content: '结束日期不可为周末',
- // })
- }
- }
- }
- }
- checkWeekend(beginDay, endDay, days) {
- var weekEndCount = 0;
- if (days < 1) {
- //判断开始时间是否是周末
- if (beginDay.getDay() == 6 || beginDay.getDay() == 0) {
- weekEndCount += 1;
- }
- } else if (days < 7) {
- if (beginDay.getDay() < endDay.getDay() && endDay.getDay() == 6) {
- weekEndCount += 1;
- }
- else if (beginDay.getDay() < endDay.getDay() && beginDay.getDay() == 0) {
- weekEndCount += 1;
- } else if (beginDay.getDay() > endDay.getDay()) {
- weekEndCount += 2;
- }
- }
- else if (days >= 7) {
- if (days % 7 > 1) {
- //以周六为界限,判断开始时间和周六的差值与总天数除以7的余数的关系判断是否需要增加额外的周末
- if (6 - beginDay.getDay() == days % 7) {
- weekEndCount += parseInt(days / 7) * 2;
- } else if (6 - beginDay.getDay() + 1 == days % 7) {
- weekEndCount += parseInt(days / 7) * 2;
- weekEndCount += 1;
- } else {
- weekEndCount += parseInt(days / 7) * 2;
- weekEndCount += 2;
- }
- } else {
- weekEndCount += parseInt(days / 7) * 2;
- }
- }
- return weekEndCount;
- },
复制代码
|