Doing time subtraction with jQuery

I have two time values ​​per page:

8:00 22:30 

I need to subtract 8 from 22:30 and get 14:30.

Is there a direct way to do this in jQuery? Plugins are ok if needed.

Update : The second number is the total, so it could be something like

 48:45 

Everything that I subtract from it should simply subtract the value from it as a total, and not perform calculations related to the date.

+4
source share
5 answers

You can do it in javascript.

Suppose you have start = '8:00' and end = '22:30' . The code is as follows:

 <script type="text/javascript"> var start = '8:00'; var end = '23:30'; s = start.split(':'); e = end.split(':'); min = e[1]-s[1]; hour_carry = 0; if(min < 0){ min += 60; hour_carry += 1; } hour = e[0]-s[0]-hour_carry; diff = hour + ":" + min; alert(diff); </script> 

After all, diff is your time difference.

+6
source

I think try this code -:

  var start = '8:00'; var end = '23:30'; var startDate = new Date("1/1/1900 " + start _time); var endDate = new Date("1/1/1900 " + end ); var difftime=endDate - startDate; //diff in milliseconds 
+2
source

jQuery is not required, you can do it in direct JavaScript with something simple:

 function timeInHours(str) { var sp = str.split(":"); return sp[0] + sp[1]/60; } function hoursToString(h) { var hours = floor(h); var minutes = (h - hours)*60; return hours + ":" + minutes; } var time1 = "08:00"; var time2 = "22:30"; var tot = hoursToString(timeInHours(time2) - timeInHours(time1)); 
+1
source

jquery does not have built-in date / manipulation parsing. I also have not seen many plugins for this. It would be best to create a function that converts two dates to int, adds, and then turns the result into a date.

0
source

Are you looking for something like Timeago ?

0
source

All Articles