VBA Function: DateDiff

The VBA function DateDiff returns an integer value corresponding to the specified time interval (seconds, minutes, hours, days, weeks, months, or years) between two dates.


DateDiff(interval, date1, date2)

Example of Usage

Using the DateDiff function with different time intervals:

Sub example()

    date1 = #10/30/2024#
    date2 = #10/30/2026#

    'Difference in days
    MsgBox DateDiff("d", date1, date2) 'Returns: 730

    'Difference in hours
    MsgBox DateDiff("h", date1, date2) 'Returns: 17520

    'Difference in minutes
    MsgBox DateDiff("n", date1, date2) 'Returns: 1051200

    'Difference in seconds
    MsgBox DateDiff("s", date1, date2) 'Returns: 63072000

    'Difference in months
    MsgBox DateDiff("m", date1, date2) 'Returns: 24

    'Difference in years
    MsgBox DateDiff("yyyy", date1, date2) 'Returns: 2

    'Difference in weeks
    MsgBox DateDiff("w", date1, date2) 'Returns: 104

End Sub