DateDiff

Returns the numeric difference in the requested date part between two dates

Method Signature

DateDiff(datepart=[string], date1=[any], date2=[any])

Arguments

Argument
Type
Required
Description
Default

datepart

string

true

date1

any

true

The reference date object

date2

any

true

The date which to compare against date1

Examples

dateDiff Example

Find the difference between two dates.

Run Example

dateDiff( "d", "2013-01-15", "2013-01-25" );

Result: 10

How old are they?

Calculates a persons age based on a variable birthDate which contains a date. Uses the now function to get current date.

Run Example

birthDate = createDate( 1972, 5, 20 );
age = dateDiff( "yyyy", birthDate, now() );
writeoutput( age );

dateDiff member function

Run Example

testDate = now();
diffDate = dateAdd( "d", 1, testDate );
writeOutput( testDate.diff( "d", diffDate ) );

Additional Examples

Run Example

echo( DateDiff( "d", "2016-06-27", "2016-06-28" ) & "<br>" ); // 1
echo( DateDiff( "d", "2016-06-27", "2016-06-27" ) & "<br>" ); // 0
echo( DateDiff( "d", "2016-06-27", "2016-06-26" ) & "<br>" ); // -1
echo( DateDiff( "h", "2016-06-27 00:00:00", "2016-06-27 01:00:00" ) & "<br>" ); // 1
echo( DateDiff( "h", "2016-06-27 00:00:00", "2016-06-27 00:00:00" ) & "<br>" ); // 0
echo( DateDiff( "h", "2016-06-27 01:00:00", "2016-06-27 00:00:00" ) & "<br>" ); // -1
echo( DateDiff( "wd", "2016-06-27 00:00:00", "2016-07-27 01:00:00" ) & "<br>" );
 // 22

Last updated

Was this helpful?