DateDiff

Top  Previous  Next

S

A

W

WS

ü

ü

Syntax -

DateDiff <variable name> <DateValue1> <DateValue2> {"DAYS"|"WEEKS"|"MONTHS"|"YEARS"|"MINUTES"|"HOURS"}

        Where:

       <variable name> is the user defined name that is assigned to the variable.

       <DateValue1> is a literal string that represents a valid date or a variable that contains a date that will be subtracted from.

       <DateValue2> is a literal string that represents a valid date or a variable that contains a date that will be subtracted.

 

Description:

This command is used to determine the number of days, weeks, months, or years between <DateValue1> and <DateValue2>.  The resulting number is stored as <variable name>.

When using the “DAYS” option, this function internally subtracts the number of days between <DateValue1> and <DateValue2> storing the resulting number as <variable name>.  

When using the “WEEKS” option, this function internally subtracts the number of days between <DateValue1> and <DateValue2> and then divides the result by 7 and stores the integer portion of the result as <variable name>.

Using the “MONTHS” and “YEARS” options does not  internally subtracts the number of days between <DateValue1> and <DateValue2>.  The resulting number is calculated as the number of calendar MONTHS or YEARS that have changed between the two dates.   Please see the sample below.

If <DateValue1> is greater than <DateValue2>, the result placed in <variable name> is positive.  If DateValue2 is greater than DateValue1 then the result is negative.

 

Note: To calculate the number of 30 day periods between <DateValue1> and <DateValue2>, use the DATEDIFF command with the “DAYS” option and then divide the result by 30.

 

Sample 1:

       The following statement uses DateDiff to find out how many days are between the “3/10/2004” and “2/10/2004”. The result of 19 is stored as DiffValue.

DateDiff DiffValue "3/10/2004" "2/20/2004" "DAYS"

 

 

Sample 2:

       The following statements of code use DateDiff to find out how many calendar year changes have occurred between the two dates.

DateDiff DiffValue "1/1/2004" "12/31/2003" "YEARS"   (The result is 1)

DateDiff DiffValue "1/1/2004" "12/31/2004" "YEARS"   (The result is 0)