I want to calculate hours between 22:00(today) and 06:00(tomorrow) with javascript or jquery. If start time = 23:10 and stop time = 05:20 working time to be 06:10, because from 23:10(today) to 05:20(tomorrow) there are 06:10 hours.
I have two input fields:
<input type="text" name="startTime" autocomplete="off" style="display:inline;width: 90px;" id="startTime" class="timepicker"/>
<input type="text" name="stopTime" autocomplete="off" style="display: inline;width: 90px;" id="stopTime" class="timepicker1"/>
My javascript code is:
var start_time = $('#startTime').val();
var end_time = $('#stopTime').val();
var diff = ( new Date("1970-1-1 " + end_time) - new Date("1970-1-1 " + start_time) ) / 60000 / 60;
var decimalTimeString = diff;
var n = new Date(0,0);
n.setSeconds(+decimalTimeString * 60 * 60);
var test = n.toTimeString().slice(0, 8);
$('#workingValue').val(test);
This javascript code works but only calculate hours between 00:00 to 23:45 e.g. if start time = 00:00 and stop time = 23:45 workingValue = 23:45 which is correct. But if start time is 22:00(for exampe) stop time is 01:00 my working value is not correct. It returning me 00:00 but has to be 03:00. Help me to resolve the problem please! P.S. Sorry for my English, but it is not very good!