Determines the number of days between two dates.

Syntax

JSV_dateDiff(expD1, expD2 [, type])

Prototype

expD1.dateDiff(expD2 [, type])

Returns

The dateDiff function returns the difference of the two dates passed to the function.

Parameters

The dateDiff function uses the following arguments.

Argument Description
expD1 The first date object.
expD2 The second date object.
type Optional. Determines the format of the returned value.
  • D - Integer number of days between the two dates. (default)
  • d - Number of days between the two dates.
  • h - Number of hours between the two dates.
  • m - Number of minutes between the two dates.
  • s - Number of seconds between the two dates.
  • ms - Number of milliseconds between the two dates.

Remarks

The dateDiff function returns the absolute value of the difference between the two dates passed to the function; therefore, the order in which the dates are passed does not matter.

Dependencies

n/a

Example

The following lines of code

var myDate1 = "1/1/2000";
var myDate2 = "1/1/2002";
document.write( "From " + myDate1 + " to " + myDate2 + " is " +
    JSV_dateDiff(JSV_convertDateString(myDate1,1), JSV_convertDateString(myDate2,1)) +
    " days.");

var dateNow = new Date();
var y2000 = new Date(2000,0,1,0,0,0);
document.write( "There are " + 
    JSV_dateDiff(dateNow, y2000, 'h') +
    " hours " +
    (dateNow > y2000 ? "in" : "until" ) +
    " the year 2000.");

produce