我想用javascript计算ot小时数,但这似乎是一段很长的代码。不是整个小时,到了半个小时就变得复杂了。到目前为止,这是我的代码。
<script>
$(function(){
$('#intime,#outtime').on('input', function() {
// declare variables
var intime = new moment($("#intime").val(), 'HH:mm:ss'); // get value of intime
var outtime= new moment($("#outtime").val(), 'HH:mm:ss');
var startTime = new moment("08:00:00", 'HH:mm:ss');
var halfDay = new moment("12:30:00", 'HH:mm:ss');
var wholeDay = new moment("18:00:00", 'HH:mm:ss');
var othours = 0; // declare day as 0
/*AFTER 18.00*/
if (moment(intime).hour() == 8 && (moment(outtime).hour() == 18 && moment(outtime).minute() == 30)) {
othours = 0.5;
}
else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 19 && moment(outtime).minute() == 30)) {
othours = 1.5;
}
else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 20 && moment(outtime).minute() == 30)) {
othours = 2.5;
}
else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 21 && moment(outtime).minute() == 30)) {
othours = moment(outtime).hour() && moment(outtime).minute() - moment(wholeDay).hour() && moment(wholeDay).minute();
}
else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 22 && moment(outtime).minute() == 30)) {
othours = 4.5;
}
else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 23 && moment(outtime).minute() == 30)) {
othours = 5.5;
}
else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 00 && moment(outtime).minute() == 30)) {
othours = 6.5;
}
/*BEFORE 8.00*/
else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 00 && moment(outtime).minute() == 30)) {
othours = 6.5;
}
else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 00 && moment(outtime).minute() == 30)) {
othours = 6.5;
}
/*WHOLE HOURS*/
else if (moment(intime).hour() == 8 && moment(outtime).hour() > 18) {
othours = moment(outtime).hour() - moment(wholeDay).hour();
}
else if (moment(intime).hour() < 8 && moment(outtime).hour() > 18) {
othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour();
}
else if (moment(intime).hour() < 8 && moment(outtime).hour() == 18) {
othours = moment(startTime).hour() - moment(intime).hour();
}
$("#othours").val(othours);
});
})
</script>有没有更短的方法,或者我还得像这样继续半个小时?在if intime= 6.30和outtime= 19.30中,其他时间应该是3小时。

这是我的输出。我想在进入进站和出站时间时计算加班时数
发布于 2017-05-08 17:03:51
试试这个-
<script>
$(function(){
$('#intime,#outtime').on('input', function() {
// declare variables
var intime = new moment($("#intime").val(), 'HH:mm:ss'); // get value of intime
var outtime= new moment($("#outtime").val(), 'HH:mm:ss');
var othours = 0; // declare day as 0
othours = moment.utc(moment(outtime,"DD/MM/YYYY HH:mm:ss").diff(moment(intime,"DD/MM/YYYY HH:mm:ss"))).format("HH")
if(othours>10)
$("#othours").val(othours-10);
else
$("#othours").val("Whole Day not work");
});
})
</script>发布于 2017-05-08 17:11:42
这与我正在寻找的东西是一致的。
<script>
$(function(){
$('#intime,#outtime').on('input', function() {
// declare variables
var intime = new moment($("#intime").val(), 'HH:mm:ss'); // get value of intime
var outtime= new moment($("#outtime").val(), 'HH:mm:ss');
var startTime = new moment("08:00:00", 'HH:mm:ss');
var wholeDay = new moment("18:00:00", 'HH:mm:ss');
var othours = 0; // declare othours as 0
if (moment(intime).hour() == 8 && (moment(outtime).hour() > 18 && moment(outtime).minute() == 30)) {
othours = moment(outtime).hour() - moment(wholeDay).hour() + 0.5;
}
else if (moment(intime).hour() == 8 && moment(outtime).hour() > 18) {
othours = moment(outtime).hour() - moment(wholeDay).hour();
}
else if ((moment(intime).hour() < 8 && moment(intime).minute() == 30) && (moment(outtime).hour() > 18 && moment(outtime).minute() == 30)) {
othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour();
}
else if ((moment(intime).hour() < 8 && moment(intime).minute() == 30) && moment(outtime).hour() == 18) {
othours = moment(startTime).hour() - moment(intime).hour() - 0.5;
}
else if (moment(intime).hour() < 8 && moment(outtime).hour() == 18) {
othours = moment(startTime).hour() - moment(intime).hour();
}
else if ((moment(intime).hour() < 8 && moment(intime).minute() == 30) && moment(outtime).hour() > 18) {
othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour() - 0.5;
}
else if (moment(intime).hour() < 8 && (moment(outtime).hour() > 18) && moment(outtime).minute() == 30) {
othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour() + 0.5;
}
$("#othours").val(othours);
});
})
</script>https://stackoverflow.com/questions/43841756
复制相似问题