VB DateDiff Function: Usage & Examples

In VB, the DateDiff function is used to calculate the time interval between two dates. The syntax of this function is as follows:

Calculate the difference in time between two dates using the specified interval.

The interval parameter is a string that specifies the type of time interval to be calculated, such as “yyyy” for years, “m” for months, “d” for days, etc. Date1 and date2 are the two dates to be compared.

For example, to calculate the number of days between two dates, you can use the following code:

Dim startDate As Date
Dim endDate As Date
Dim daysDiff As Integer

startDate = #1/1/2021#
endDate = #1/10/2021#

daysDiff = DateDiff("d", startDate, endDate)
MsgBox "Days difference: " & daysDiff

This code will calculate the number of days between January 1, 2021, and January 10, 2021, and display the result in a message box.

bannerAds