DateDiff (function)

Syntax

DateDiff(interval$,date1,date2)

Description

Returns a Date variant representing the number of given time intervals between date1 and date2.

Comments

The following table describes the parameters:

 

Parameter

Description

 

Interval$

String expression indicating the specific time interval you wish to find the difference between.

 

Date1

Any expression convertible to a Date. An example of a valid date/time string would be "January 1, 1994".

 

Date2

Any expression convertible to a Date. An example of a valid date/time string would be "January 1, 1994".

 

The following table lists the valid time interval strings and the meanings of each. The Format$ function uses the same expressions.

 

Time

Interval

 

"y"

Day of the year

 

"yyyy"

Year

 

"d"

Day

 

"m"

Month

 

"q"

Quarter

 

"ww"

Week

 

"h"

Hour

 

"n"

Minute

 

"s"

Second

 

"w"

Weekday

 

To find the number of days between two dates, you may use either day or day of the year, as they are both equivalent ("d", "y").

 

The time interval weekday ("w") will return the number of weekdays occurring between date1 and date2, counting the first occurrence but not the last. However, if the time interval is week ("ww"), the function will return the number of calendar weeks between date1 and date2, counting the number of Sundays. If date1 falls on a Sunday, then that day is counted, but if date2 falls on a Sunday, it is not counted.

The DateDiff function will return a negative date/time value if date1 is a date later in time than date2.

Example

This example gets today's date and adds ten days to it. It then calculates the difference between the two dates in days and weeks and displays the result.

Sub Main()

  Today$ = Format(Date$,"Short Date")
  NextWeek = Format(DateAdd("d",14,today$),"Short Date")
  DifDays# = DateDiff("d",today$,NextWeek)
  DifWeek# = DateDiff("w",today$,NextWeek)
  s$ = "The difference between " & today$ & " and " & NextWeek
  s$ = s$ & " is: " & DifDays# & " days or " & DifWeek# & " weeks"
  MsgBox s$
End Sub

See Also

DateAdd (function).

More information

D