DateDiff Function

The DateDiff function returns the difference of two date values. In other words, this function subtracts two date values and returns it in the format chosen: year, day, month, hour, or minute. A good use case is using the DateDiff function to determine the age with a birthdate and today’s date.

Syntax:



DateDiff(1,2,3)
1 = First date value to use in equation (can be a variable). Valid values include ‘MM/dd/yyyy’ or ‘YYYY-MM-DD’
2 = Second date value to subtract from (can be a variable). Valid values include ‘MM/dd/yyyy’ or ‘YYYY-MM-DD’
3 = Unit of time specified Valid values include ‘Y’ (year), ‘M’ (month), ‘D’ (day), ‘H’ (hour), ‘MI’ (minute)

Example:



%%[
var @date, @birthdate, @age
set @date = Now()
set @birthdate = “01/01/1991”
set @age = DateDiff(@birthdate, @date, “Y”)


]%%
Age: %%=v(@age)=%%

Output:

Age: 30

Explanation:

Using the DateDiff function, the two dates: birthdate and today’s date (using the Now function) are used in the equation to determine the age. The unit time of difference used in this example is “Y” which returns the number of years as the difference.

Leave a Reply

Your email address will not be published. Required fields are marked *