I have two <input type="time">
. By default, each input collects a time value as a string. For example, "08:30"
.
How can I convert this string into an object which would then enable computation? Is it possible to avoid involving the use of date
in this approach?
In the end, I would like to compute the difference between two time
strings and then return the result in minutes. For example, the expected return value of08:00
and 09:00
would be 60
minutes.
Assuming you want to use a 24h clock:
Just do it as if you only had pen and paper:
You can parse from string using regular expressions:
... and formatting back to string should not be very difficult either.
Generally speaking I whould suggest using Date insted of using custom functions, but just for your case this is the example function:
Note that this function is not responsible for validation of the time difference only the computation. You should validate you input