0

I have this function

function calculateTimes(firstDate, firstTime, secondDate, secondTime){
    var fDate = firstDate+" "+firstTime;
    var sDate = secondDate+" "+secondTime;

    var firstDateFull = new Date(fDate);
    var secondDateFull = new Date(sDate);

    var diff = secondDateFull - firstDateFull;

    var diffSeconds = diff / 1000;
    var HH = Math.floor(diffSeconds / 3600);
    var MM = Math.floor((diffSeconds % 3600) / 60);
    var SS = "00";

    var formatted = ((HH < 10) ? ("0" + HH) : HH) + ":" + ((MM < 10) ? ("0" + MM) : MM);

    return formatted;
}  

Which I use like this

    $('#uoc-r4disDate-fld, #uoc-r4disTime-fld, #uoc-disDate-fld, #uoc-disTime-fld').on('change', function() {
        $("#uoc-delay-fld").val(calculateTimes($("#uoc-r4disDate-fld").val(), $("#uoc-r4disTime-fld").val(), $("#uoc-disDate-fld").val(), $("#uoc-disTime-fld").val()));
    });

My problem is that this is a delay field and the hours can run up past 24h. Is there a way to make a Time field accept more than 24h in the hour in JavaScript? The database im using ( 4th Dimension ) can accept hours above 24h for exactly this type of situation I guess.

So, it need to be able to display - 26:34. Elapsed time in time format, like a stopwatch.

morne
  • 4,035
  • 9
  • 50
  • 96
  • For such things use moment.js – Harry Joy Oct 19 '16 at 09:15
  • 1
    Possible duplicate of [Work with a time span in Javascript](http://stackoverflow.com/questions/14297625/work-with-a-time-span-in-javascript) – user247702 Oct 19 '16 at 09:16
  • So, do you want to display "25h" or "1d 1h" instead…? Also, if you actually understand the algorithm you've implemented there, it shouldn't be too difficult to adjust it either way. – deceze Oct 19 '16 at 09:16
  • Yeah, need to display 25h up – morne Oct 19 '16 at 09:28
  • @deceze. Okay, you understand it so well that you care to explain? The DB CAN except a time format of `27:03`, but JavaScript or HTML not. Im just asking a simple question. – morne Oct 19 '16 at 09:42
  • At what point exactly do you want to accept "27:03"? As input `firstTime` and `secondTime`, or as the return value `formatted`? – deceze Oct 19 '16 at 09:44
  • What do you mean by "a Time field"? – melpomene Oct 19 '16 at 09:44
  • Elapsed time greater than 24h. So the return value will be greater than 24h. – morne Oct 19 '16 at 09:47
  • By Time Filed I mean, a database table field as type `Time`. – morne Oct 19 '16 at 09:47
  • You said your database can accept hours above 24, so your problem is already solved, isn't it? – melpomene Oct 19 '16 at 09:50
  • ERROR in CONSOLE: The specified value "30:03" does not conform to the required format. The format is "HH:mm", "HH:mm:ss" or "HH:mm:ss.SSS" where HH is 00-23, mm is 00-59, ss is 00-59, and SSS is 000-999. - So it might be HTML and not JavaScript. Not sure. – morne Oct 19 '16 at 09:52
  • What HTML? Your question doesn't say anything about an HTML page. – melpomene Oct 19 '16 at 09:54

1 Answers1

6

If I get everything right, than you are using <input type="time" /> as »timefield«. This input type is designed to handle day times, not time spans. Those daytimes can be formatted in 12, or 24 hour format, and they all describe a time of a day. Something like "35:54" is a timespan of 35 hours and 54 Minutes and cannot be used to describe a certain time of a day, so such a value is not valid for this type of input.

If you need an input for such values, you can of course use one <input type='text' />, or two <input type="number" />, one for the hours and one for the minutes, like so:

<div id="time-span">
    <input type="number" min="0" step="1" />:<input type="number" min="0" max="59" step="1" />
</div>
philipp
  • 15,947
  • 15
  • 61
  • 106
  • @morne Your welcome. Just remind to always post all relevant pieces of code in your next questions ;) – philipp Oct 19 '16 at 10:04