0

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!

  • `It returning me 00:00` are you sure? I'd have thought it'd be negative ... check if `diff` is negative ... if so, add 24 to it – Jaromanda X Oct 08 '18 at 11:56
  • Possible duplicate of [javascript - time counter since start date and time](https://stackoverflow.com/questions/26049855/javascript-time-counter-since-start-date-and-time) – cнŝdk Oct 08 '18 at 12:11
  • You can see the demo here. Mb hear you're answer - http://jsfiddle.net/c0rxkhyz/1/ – Maxim Strutinskiy Oct 08 '18 at 12:11

3 Answers3

1

you can use moment
in the snippet you can show an example

let now  = "10/10/2018 15:00:00";
let then = "07/10/2018 14:20:30";

let ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
let d = moment.duration(ms);
let s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

document.getElementById("time").innerText = s
<script src="https://momentjs.com/downloads/moment.js"></script>

<div id="time"></div>
1

Using the information from your post, i.e. you only have two time strings without date information, you can do the following in Vanilla JavaScript:

const ge=id=>document.getElementById(id);
const res=ge('result'), st=ge('start'), en=ge('end');
document.forms[0].addEventListener('keyup',function(el){
  res.innerHTML=new Date(new Date('1970-1-2 '+en.value)
                        -new Date('1970-1-1 '+st.value)).toGMTString().substr(17,5);
})
<form>
<input id="start" value="22:30"> start<br>
<input id="end" value="2:00"> end<br>
</form>
<div id="result"></div>
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

When you calculate this calculation with 00:00 and 23:45 it gives positive result so okay but in opposite case let's say 23:45 00:00 it will give minus result

var diff = ( new Date("1970-1-1 " + end_time) - new Date("1970-1-1 " + start_time) ) / 60000 / 60;
Rarblack
  • 4,559
  • 4
  • 22
  • 33