i want to calculate ot hours in javascript but it seems like it's a very long code. not for whole hours, when it comes to half hours it gets complicated. this is my code so far.
<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>
is there any shorter method or do i have to continue like this for other half hours also? as in if intime= 6.30 and outtime= 19.30, the othours should be 3hours.
Aucun commentaire:
Enregistrer un commentaire