Skip to content

Instantly share code, notes, and snippets.

@zenxds zenxds/holiday.js
Last active Aug 29, 2015

Embed
What would you like to do?
/**
* 开始时间,结束时间
* isOnlyWorkDay 工作时间是否只计算工作日
* 婚假这些周六周日都算进去的 事假这些就不算周末
*/
(function() {
var MILLISECONDS_OF_MINUTE = 60 * 1000;
var MILLISECONDS_OF_DAY = 24 * 60 * MILLISECONDS_OF_MINUTE;
var startWorkMinutes = 570, // 九点半
//
endWorkMinutes = 1080;
// 返回天
function holiday(start, end, isOnlyWorkDay) {
var ret = 0;
if (start >= end) {
return ret;
}
var minutes = 0,
formType = $("[name=formTypeId]").val(),
// 加班
isOvertime = formType == "12";
startTime = start.getTime(),
endTime = end.getTime();
if (isOvertime) {
isOnlyWorkDay = true;
}
var date;
// 在同一天内
if (isInSameDay(start, end)) {
if (!isRest(start, isOnlyWorkDay)) {
minutes += getWorkTime({
start: start,
end: end
});
}
} else {
if (!isRest(start, isOnlyWorkDay)) {
minutes += getWorkTime({
start: start
});
}
while (startTime < endTime) {
startTime += MILLISECONDS_OF_DAY;
date = new Date(startTime);
if (isInSameDay(date, end)) {
break;
} else {
if (!isRest(date, isOnlyWorkDay)) {
minutes += endWorkMinutes - startWorkMinutes;
}
}
}
if (!isRest(end, isOnlyWorkDay)) {
minutes += getWorkTime({
end: end
});
}
}
// 加班时间反一下
if (isOvertime) {
minutes = (end - start) / MILLISECONDS_OF_MINUTE - minutes;
}
var dx;
// 加班
if (isOvertime) {
// 加班至少3小时
// 加班8小时算1天
ret = Math.floor(minutes / 480);
dx = minutes % 480;
// 3-8小时算0.5天
if (dx >= 180 && dx < 480) {
ret += 0.5;
}
} else {
// 请假
ret = Math.floor(minutes / 510);
dx = minutes % 510;
// 5小时以内算0.5天
if (dx) {
if (dx <= 300) {
ret += 0.5;
} else {
ret += 1;
}
}
}
return ret;
}
// 计算两个时间内的工作时间: 分钟
// 这两个时间应该在一天内
function getWorkTime(config) {
config = config || {};
var normalized;
if (!config.start) {
normalized = normalizeDate(config.end);
config.start = new Date(normalized.Y, normalized.M - 1, normalized.D, 9, 30);
}
if (!config.end) {
normalized = normalizeDate(config.start);
config.end = new Date(normalized.Y, normalized.M - 1, normalized.D, 18);
}
var minutes,
// 开始结束的分钟数
startMinutes = getMinutes(config.start),
endMinutes = getMinutes(config.end);
// 请假的时间要在工作时间内
if (startMinutes > endWorkMinutes || endMinutes < startWorkMinutes) {
minutes = 0;
} else {
minutes = (endMinutes > endWorkMinutes ? endWorkMinutes : endMinutes) - (startMinutes < startWorkMinutes ? startWorkMinutes: startMinutes);
}
return minutes;
}
/**
* 判断两个日期是否在同一天
* @return {Boolean} [description]
*/
function isInSameDay(date1, date2) {
var format1 = formatDate(date1),
format2 = formatDate(date2);
return format1.slice(0, 10) === format2.slice(0, 10);
}
function formatDate(date) {
var normalized = normalizeDate(date);
return "Y-m-d h:i:s".replace(/y|m|d|h|i|s/gi, function(k) {
return normalized[k];
});
}
function normalizeDate(date) {
var o = {
Y: date.getFullYear(),
M: date.getMonth() + 1,
D: date.getDate(),
H: date.getHours(),
I: date.getMinutes(),
S: date.getSeconds()
};
var ret = {};
for (var k in o) {
// 统一结果为字符串
ret[k] = o[k] + "";
ret[k.toLowerCase()] = padding2(o[k]).slice(-2);
};
return ret;
}
function padding2(str) {
str += "";
return str.length === 1 ? "0" + str : str;
}
/**
* 是否是休息时间
*/
function isRest(date, isOnlyWorkDay) {
var day = date.getDay();
return (day === 0 || day === 6) && isOnlyWorkDay;
}
/**
* 获取一个日期的一天总分钟数
*/
function getMinutes(date) {
var hour = date.getHours();
return hour * 60 + date.getMinutes();
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.