Determining time elapsed between 2 dates
<p>I am trying to determine the time elapsed between 2 dates using javascript.  An example would be: "I quit smoking on January 5, 2008 at 3 A.M., how many years, months, and hours has elapsed since I quit?".</p>

<p>So my thoughts were:</p>

<ol>
<li>Get "quit" date</li>
<li>Get current date</li>
<li>Convert to time (milliseconds)</li>
<li>Find the difference</li>
<li>Create a new date using the difference</li>
<li>Extract the years, months, etc. from that date</li>
</ol>

<p>Well, it is acting strange and I can't pin point why.  Any insight?</p>

<pre><code>//create custom test date
var d1 = new Date(2012, 8, 28, 13, 14, 0, 0);
//create current date
var d2 = new Date();
//get date times (ms)
var d1Time = (d1.getTime());
var d2Time = (d2.getTime());
//calculate the difference in date times
var diff = d2 - d1;
//create a new date using the time differences (starts at Jan 1, 1970)
var dDiff = new Date();
dDiff.setTime(diff);
//chop off 1970 and get year, month, day, and hour
var years = dDiff.getFullYear() - 1970;
var months = dDiff.getMonth();
var days = dDiff.getDate();
var hours = dDiff.getHours();
</code></pre>

<p>You can see it in action at <a href="http://pastehtml.com/view/cdu6voso4.html" rel="nofollow">this temporary host</a>.</p>
