<p>I doing a function in Javascript like the VisualBasic DateDiff.</p>

<p>You give two dates and the returning time interval (Seconds, Minutes, Days, etc...)</p>

<pre><code>DateDiff(ByVal Interval As Microsoft.VisualBasic.DateInterval, _
  ByVal Date1 As Date, ByVal Date2 As Date) as Long
</code></pre>

<p>So what's the best way to calculate the difference of Javascript Dates?</p>

If you follow this
tutorial
, one way is to use:
```
```
Date.getTime()
```
```
You will find a full
javascript function here
, complete with date validation.
That being said, as commented by
Rafi B.
5 years later, "
Get difference between 2 dates in javascript?
" is more precise.
```
```
var _MS_PER_DAY = 1000 * 60 * 60 * 24;

// a and b are javascript Date objects
function dateDiffInDays(a, b) {
  // Discard the time and time-zone information.
  var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
  var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

  return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
```
```