title: How to calculate the time difference between 2 date time values
body: <p>I am trying to calculate the time difference between 2 date time strings.</p>
  
  <p>I have 2 inputs where the input string is something like this "1:00 PM" and the second one "3:15 PM". I want to know the time difference. So for the above example I want to display 3.15</p>
  
  <p>What I have done:</p>
  
  <ol>
  <li>Converted the time to a 24 hours format. So "1:00 PM" becomes "13:00:00"</li>
  <li>Appended the new time to a date like so: <code>new Date("1970-1-1 13:00:00")</code></li>
  <li>Calculated the difference like so:</li>
  </ol>
  
  <p>Code:</p>
  
  <pre><code>var total = Math.round(((new Date("1970-1-1 " + end_time) - 
                           new Date("1970-1-1 " + start_time) ) / 1000 / 3600) , 2 )
  </code></pre>
  
  <p>But the total is always returning integers and not decimals, so the difference between "1:00 PM" and "3:15 PM" is 2 not 2.15.</p>
  
  <p>I have also tried this (using jQuery, but that is irrelevant):</p>
  
  <pre><code>$('#to_ad,#from_ad').change(function(){
      $('#total_ad').val( getDiffTime() );
  });
  
  function fixTimeString(time){
      var hours = Number(time.match(/^(\d+)/)[1]);
      var minutes = Number(time.match(/:(\d+)/)[1]);
      var AMPM = time.match(/\s(.*)$/)[1];
      if(AMPM == "PM" &amp;&amp; hours&lt;12) hours = hours+12;
      if(AMPM == "AM" &amp;&amp; hours==12) hours = hours-12;
      var sHours = hours.toString();
      var sMinutes = minutes.toString();
      if(hours&lt;10) sHours = "0" + sHours;
      if(minutes&lt;10) sMinutes = "0" + sMinutes;
      return sHours + ':' + sMinutes + ':00';
  }
  
  function getDiffTime(){
      var start_time = fixTimeString($('#from_ad').val());
      var end_time = fixTimeString($('#to_ad').val());
      var start = new Date("1970-1-1 " + end_time).getTime(),
      end   = new Date("1970-1-1 " + start_time).getTime();
      return parseInt(((start  - end) / 1000 / 3600, 10)*100) / 100;
  }
  </code></pre>
  
  <p>But the <code>total_ad</code> input is displaying only integer values.</p>
  
  <p>How can I fix this problem?</p>