Note that there are some explanatory texts on larger screens.

plurals
  1. PODifference between times in JavaScript
    primarykey
    data
    text
    <p>I found the following code which is working well for a time sheet calculator I am using</p> <pre><code>function convertSecondsToHHMMSS(intSecondsToConvert) { var hours = convertHours(intSecondsToConvert); var minutes = getRemainingMinutes(intSecondsToConvert); minutes = (minutes == 60) ? "00" : minutes; var seconds = getRemainingSeconds(intSecondsToConvert); return hours+" hrs "+minutes+" mins"; } function convertHours(intSeconds) { var minutes = convertMinutes(intSeconds); var hours = Math.floor(minutes/60); return hours; } function convertMinutes(intSeconds) { return Math.floor(intSeconds/60); } function getRemainingSeconds(intTotalSeconds) { return (intTotalSeconds%60); } function getRemainingMinutes(intSeconds) { var intTotalMinutes = convertMinutes(intSeconds); return (intTotalMinutes%60); } function HMStoSec1(T) { var A = T.split(/\D+/) ; return (A[0]*60 + +A[1])*60 + +A[2] } var time1 = HMStoSec1("10:00:00"); var time2 = HMStoSec1("12:05:00"); var diff = time2 - time1; document.write(convertSecondsToHHMMSS(diff)); </code></pre> <p>It works fine when time1 is greater than time2, if time1 is less than time2 an extra hour is subtracted eg. </p> <pre><code>var time1 = HMStoSec1("09:00:00"); var time2 = HMStoSec1("08:55:00"); var diff = time2 - time1; document.write(convertSecondsToHHMMSS(diff)); // writes "1 hr 5 mins" instead of "0 hr 5 mins" </code></pre> <p>I think its something to do with the Math.floor in the convertHours function.</p> <p>I am trying to build something that can just take hours and minutes and subtract/add time, not actually times just quantities of hours and minutes.</p> <p>There must be a simpler way that sadly eludes me, any help would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload