0

I have a calculator that calculates the time needed to heat water given the temperature of the water, desired temperature, water volume, energy applied, and thermal loss. The output is in seconds. I also have a field where the user can input a time. How can I take that time and subtract the seconds from it?

JavaScript:

    function calculate() {  
       var liters = document.getElementById("liters").value;
       var currentTemp = document.getElementById("currentTemp").value;
       var desiredTemp = document.getElementById("desiredTemp").value;
       var elementWattage = document.getElementById("elementWattage").value;
       var thermalLoss = document.getElementById("thermalLoss").value;
       var mashInTime = document.getElementById("mashInTime").value;

       var secondsToHeat = ((4186 * liters ((desiredTemp - currentTemp) * thermalLoss) / elementWattage);

}

HTML:

<p><label for="liters">Liters:</label>
    <input id="liters" onkeyup="calculate()" value="22">
<p><label for="currentTemp">Water Temperature &deg;C:</label>
    <input id="currentTemp" onkeyup="calculate()" value="15">
<p><label for="desiredTemp">Desired Temperature &deg;C:</label>
    <input id="desiredTemp" onkeyup="calculate()" value="77">
<p><label for="elementWattage">Watts of Heating Element:</label>
    <input id="elementWattage" onkeyup="calculate()" value="1000">
<p><label for="thermalLoss">Thermal Loss (1.05 insulated, 1.1+ uninsulated):</label>
    <input id="thermalLoss" onkeyup="calculate()" value="1.11">
<p><label for="mashInTime">Mash In Time:</label>
    <input id="mashInTime" onkeyup="calculate()" value="">

Do I need to parse out the time entered? Or do I need to convert the seconds into hours/minutes and then subtract?

Thanks!

TheAleMaster
  • 83
  • 2
  • 9
  • You can try using moment.js: http://momentjs.com/. It lets you easily manipulate dates and times. – Andrew Dunai Dec 11 '14 at 22:13
  • I need a little more info, but I think this will be an easy solution. What will the format of mashInTime's value be (seconds, minutes, number, UTC? What is the purpose of mashInTime? – Adam Dec 11 '14 at 22:22
  • Sorry. Mash In Time is just the time of day in 12 hour format, i.e. 8:00. I'll probably end up using jQuery Mobile Datebox to enforce a proper time. Mash In Time is what I want to subtract the seconds from in order to tell the user at what time they should turn the heater on. – TheAleMaster Dec 11 '14 at 22:35

1 Answers1

1

You're on the right track using jQuery to simplify form validation. Once you get the time, I would convert it to seconds, that way the subtraction is fairly straightforward.

Once you have the start time in seconds, converting it back to the format you want displayed could be made easy with Moment.js. That link is to a Stack Overflow question dealing with time-formatting.

Community
  • 1
  • 1
Adam
  • 2,027
  • 1
  • 16
  • 27