I'm hoping to pull a variable from a text box that would be submitted like "201442" turn it into time 20:14:42 then subtract 5 seconds for the output 20:14:37. So far i have:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>-5</title>
<script>
var incomingtime = "0";
</script>
<script>
function calctime(){
incomingtime = document.getElementById("inputtime").value;
alert (incomingtime);
}
</script>
</head>
<body>
<input type="text" id="inputtime"/><input type="text" id="outputtime"/><br>
<button onclick="calctime()">-5 Dat</button>
</body>
</html>
What would be the best approach? Splitting up the input into three sections and doing the math that way? Or is there a simpler solution? How would i go about throwing first two chars into a variable, third and fourth into a var, then 5th and 6th into a var for calculations?
I guess, you should parse your string and put your time into Date() object, then it will be easier to do any computations on your time value You can parse it in the following way:
Then you can carry out your values, using object's 'get' functions. If you want to change its value, you can just add or substract time (in miliseconds). For example, if you want to substract 5 seconds, you should just: