JS實現請假時長計算(計算小時數差)

給公司做了一套系統,涉及到請假單功能開發。在計算請假時長這塊總結一下:按天計算的就不總結了比較簡單,這裏總結一下按小時數計算的 ,話不多說,直接上代碼

// 獲取兩個日期相差的工作小時(不包括節假日)
function getHour(StartTime, EndTime) {
	StartTime = new Date(StartTime.replace(/-/g, '/'));
	EndTime = new Date(EndTime.replace(/-/g, '/'));
	var ms = Math.abs(EndTime.getTime() - StartTime.getTime());
	// 實際工時(天) = 起止日期差 - 週六日數目。
	if ((StartTime.getMonth() == EndTime.getMonth())
			&& (StartTime.getDate() == EndTime.getDate())) {
		// 若爲同一日期
		var hour1 = (getDayHour(StartTime.getHours(), StartTime.getMinutes(),
				EndTime.getHours(), EndTime.getMinutes(), "1")).toFixed(1);
		$("#xxtj").val(hour1.substring(0,hour1.length-2));// 按天計算
		return hour1;
	} else {
		var sh=StartTime.getHours();
		var eh= EndTime.getHours();
		if (13 <= sh) {
			if (8 <= eh && eh <= 12) {
				var days = Math.ceil(ms / 1000 / 60 / 60 / 24) + 1;
			}else{
				var days = Math.floor(ms / 1000 / 60 / 60 / 24) + 1;
			}
		} else {
			var days = Math.floor(ms / 1000 / 60 / 60 / 24) + 1;
		}
		var workDayVal = 0;
		// 工時的餘數
		var remainder = days % 7;
		// 工時向下取整的除數
		var divisor = Math.floor(days / 7);
		var weekendDay = 2 * divisor;

		// 起始日期的星期,星期取值有(1,2,3,4,5,6,0)
		var nextDay = StartTime.getDay();
		// 從起始日期的星期開始 遍歷remainder天
		for (var tempDay = remainder; tempDay >= 1; tempDay--) {
			// 第一天不用加1
			if (tempDay == remainder) {
				nextDay = nextDay + 0;
			} else if (tempDay != remainder) {
				nextDay = nextDay + 1;
			}
			// 週日,變更爲0
			if (nextDay == 7) {
				nextDay = 0;
			}
			// 週六日
			if (nextDay == 0 || nextDay == 6) {
				weekendDay = weekendDay + 1;
			}
		}
		workDayVal = days - weekendDay - 2;
		var hour0 = (workDayVal * 8 + getDayHour(StartTime.getHours(),
				StartTime.getMinutes(), EndTime.getHours(), EndTime
						.getMinutes(), "0")).toFixed(1);
		$("#xxtj").val(hour0.substring(0,hour0.length-2));// 按天計算
		return hour0;
	}
}
function getDayHour(sh, sm, eh, em, type) {
	sh = parseInt(sh);
	eh = parseInt(eh);
	if (type == "0") {
		// 計算非當天
		if (8 <= sh && sh <= 12) {
			sh = 12 - sh + 4;
		} else if (13 <= sh && sh <= 17) {
			sh = 17 - sh;
		}
		if (8 <= eh && eh <= 12) {
			eh = eh - 8;
		} else if (13 <= eh && eh <= 17) {
			eh = eh - 13 + 4;
		}
		return parseFloat(sh + eh);
	} else {
		// 計算當天
		if (sh == eh) {
			// 在同一小時
			if (sm == em) {
				return 0;
			} else {
				return Math.abs((sm - em) / 60);
			}
		} else {
			// 不在同一小時
			// 開始時間在上午時間段,並且結束時間在下午時間段
			if ((8 <= sh && sh <= 12) && (13 <= eh && eh <= 17)) {
				if (sm == 30) {
					return parseFloat(12 - sh + eh - 13);
				} else if (sm < 30) {
					return parseFloat(12 - sh + eh - 13);

				} else if (sm > 30 && sh != 12) {
					return parseFloat(12 - sh - 1 + eh - 13);
				}
			}
			// 開始時間與結束時間都在上午時間段
			if ((8 <= sh && sh <= 12) && (8 <= eh && eh <= 12)) {

				if (sm == em) {
					return parseFloat(Math.abs(eh - sh));
				} else {

					return parseFloat(Math.abs(eh - sh - 1) + (60 - sm) / 60
							+ em / 60);
				}

			} else if (13 <= sh && sh <= 17 && 13 <= eh && eh <= 17) {
				if (sm == em) {
					return parseFloat(Math.abs(eh - sh));
				} else {

					return parseFloat(Math.abs(eh - sh - 1) + (60 - sm) / 60
							+ em / 60);
				}
			}
		}
	}
}

 

因爲都是整點的請,分鐘數就不算了 。如有不對的地方,歡迎指教 !

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章