0

I am trying to calculate how many minutes a worker works from the input starting and ending time(e.g. 10:30 am to 3:30pm). Could u guys help how to calculate them? Could u check my code and correct them? I am very new in Javascript.

function myFunction(){
  var sTime=document.getElementById("startTime").value;
  var eTime=document.getElementById("endTime").value;
  var diff = sTime-eTime; 
  var result= diff.getMinutes();      

  document.getElementById("demo").innerHTML=result`;

https://jsbin.com/bolapox/edit?html,output

Tushar
  • 85,780
  • 21
  • 159
  • 179

2 Answers2

0

You will need to turn the users input into a usable format with Date().parse(input). This returns the number of milliseconds since 1 January, 1970, 00:00:00, local time. You can then take the difference in milliseconds and convert them into minutes.

var sTime=Date().parse(document.getElementById("startTime").value);
var eTime=Date().parse(document.getElementById("endTime").value);
var diff = eTime - sTime;
var result = diff / 60000;
Lucas S.
  • 2,303
  • 1
  • 14
  • 20
0

You should consider Moment.js, here yiou can find some examples: http://momentjs.com/docs/#/durations/

M.K. Wierzba
  • 146
  • 7