<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script src="moment.js"></script>
<script type="text/javascript" language="javascript"> function getSharpTime(baseDate,hour,day_diff)
{
var myDateTime_tmp = baseDate;
if(day_diff != '')
{
myDateTime_tmp.add(day_diff, 'd');
}
myDateTime_tmp.hours(hour).minutes(0).seconds(0);
return myDateTime_tmp;
}
function GetWorkHours(beginDateTime, endDateTime) {
//debugger;
var _totalHour = 0;
//1、获取开始时间和结束时间之间的日
var _beginDate = moment(beginDateTime);
var _endDate = moment(endDateTime);
//整理时间
if (_beginDate.get("hour") < 8)
{
//开始时间小于8点,设置为8点
_beginDate = getSharpTime(_beginDate,8);
}
else if (_beginDate.get('hour') == 12)
{
//开始时间在12点-13点之间,设置为13点
_beginDate = getSharpTime(_beginDate,13);
}
else if (_beginDate.get("hour") >= 17)
{
//_beginDate.add(1, 'd').hour(8);
_beginDate = getSharpTime(_beginDate,8,1);
}
if (_endDate.get("hour") > 17)
{
//结束时间大于17点,设置为17点
_endDate = getSharpTime(_endDate,13);
}
else if (_endDate.get('hour') == 12)
{
//结束时间在12点-13点之间,设置为12点
_endDate = getSharpTime(_endDate,12); }
else if (_endDate.get("hour") <= 8)
{
_endDate = getSharpTime(_endDate,17,-1);
}
var _tempDateTime = _beginDate; //缓存一个日期,起始时开始时间
while (moment(_tempDateTime).isBefore(_endDate))
{
//2.1、判断是否周日,周六
//var _week = moment(_tempDateTime).weekday();
//if (_week == 0 || _week == 6) {
//是周末,不算入时间
//_tempDateTime.add(1, 'd');
//continue;
//}
if (moment(_tempDateTime).isSame(_beginDate, "day"))
{
if (moment(_tempDateTime).isSame(_endDate, "day")) {
//开始时间和结束时间是同一天,结束时间-开始时间
_totalHour += _endDate.diff(_beginDate, "hours", true);
//如果跨越中午,减去一个小时
if (moment(_tempDateTime).get('hour') <= 12 && moment(_endDate).get('hour') > 12)
{
_totalHour -= 1;
}
_tempDateTime.add(1, 'd');
continue;
}
else
{
//开始时间和结束时间不是同一天,17点减-开始时间
_totalHour += moment(_beginDate).hour(17).minute(0).second(0).diff(_beginDate, "hours", true);
//如果跨越中午,减去一个小时
if (moment(_tempDateTime).get('hour') <= 12 )
{
_totalHour -= 1;
}
_tempDateTime.add(1, 'd').hour(8).minute(0).second(0);
continue;
}
}
else if (moment(_tempDateTime).isSame(_endDate, "day"))
{
//是否和结束时间是同一天,结束时间-开始时间8点,
_totalHour += _endDate.diff(moment(_endDate).hour(8).minute(0).second(0), "hours", true);
//如果跨越中午,减去一个小时
if (moment(_endDate).get('hour') > 12 )
{
_totalHour -= 1;
}
_tempDateTime.add(1, 'd');
continue;
}
else
{
_totalHour += 8;
_tempDateTime.add(1, 'd');
continue;
}
}
// alert(_totalHour < 0 ? 0 : _totalHour.toFixed(2));
//
return _totalHour < 0 ? 0 : _totalHour.toFixed(2); }
function GetTotalWorkHours(beginDateTime1, endDateTime1,beginDateTime2, endDateTime2)
{
if(endDateTime2 == '' || beginDateTime1 == '' )
{
alert('date A 和 data D 不能为空');
}
if(endDateTime1 == '')
{
return GetWorkHours(beginDateTime1 ,endDateTime2) ;
}
else
{
if (beginDateTime2 == '')
{
return GetWorkHours(beginDateTime1 ,endDateTime1) ;
}
else
{
// alert(GetWorkHours(beginDateTime1 ,endDateTime1));
// alert(GetWorkHours(beginDateTime2 ,endDateTime2));
var _totalhours1 = parseFloat(GetWorkHours(beginDateTime1 ,endDateTime1));
var _totalhours2 = parseFloat(GetWorkHours(beginDateTime2 ,endDateTime2));
var _totalhours = _totalhours1 + _totalhours2;
return _totalhours;
}
}
//beginDateTime1(A), endDateTime1(B),beginDateTime2(C), endDateTime2(D)
/*
3 计算逻辑
当 B,C没有时,hour=D-A; 当B有C没有时 hour=B-A;
当B,C都有时 hour= (B-A)+(D-C);
*/
}
</script>
</head>
<body>
时间1(A):<input type = "text" id = "num1" value = "2016-05-28 12:01" /><br />
时间2(B):<input type = "text" id = "num2" value = "2016-05-29 14:00" /><br />时间3(C):<input type = "text" id = "num3" value = "2016-05-29 18:31" /><br />
时间4(D):<input type = "text" id = "num4" value = "2016-05-30 11:00" /><br /><hr />
<input type = "text" id = "result" value = "" />
<button οnclick="document.getElementById('result').value = GetTotalWorkHours(document.getElementById('num1').value,document.getElementById('num2').value,document.getElementById('num3').value,document.getElementById('num4').value);">测试结果</button ><br />
<input type="text" id = "b_a" value = "" />
<button οnclick="document.getElementById('b_a').value = GetWorkHours(document.getElementById('num1').value,document.getElementById('num2').value);">B-A</button><br />
<input type="text" id = "d_a" value = "" />
<button οnclick="document.getElementById('d_a').value = GetWorkHours(document.getElementById('num1').value,document.getElementById('num4').value);">D-A</button><br />
<input type="text" id = "d_c" value = "" />
<button οnclick="document.getElementById('d_c').value = GetWorkHours(document.getElementById('num3').value,document.getElementById('num4').value);">D-C</button><br />
</body>
</html>
标签:totalHour,12,辅助,hour,beginDate,js,moment,endDate,else
From: https://blog.51cto.com/u_8215601/7508974