contents.gifindex.gifprev1.gifnext1.gif

DateDiff

Purpose

Returns a Variant that contains the number of time intervals between two specified dates.

Arguments

DateDiff( interval, date1, date2[, firstweekday][, firstweek] )

DateDiff uses the following arguments:

interval
A string expression that is the interval of time you use to calculate the difference between date1 and date2.
date1, date2
Two dates you want to use in the calculation or the names of two date/time fields (or a combination).
firstweekday
An integer that specifies the first day of the week; 1=Sunday, 2=Monday, etc.
firstweek
An integer that specifies the first week of the year. It can be any of the following:
0 = Use the First Week setting in the Options dialog box
1 = Start on January 1 (default)
2 = Start with the first four-day week
3 = Start with the first full week
Notes

You can use the DateDiff function to determine how may time intervals exist between two dates. For example, you can use DateDiff to calculate the number of days between an order date and its shipdate or the number of weeks between today and the end of the year.

The exact value returned by DateDiff can depend on the settings of the First Weekday and First Week options in the Options dialog box (View menu) or by the values of the firstweekday or firstweek arguments. For example, if you set firstweekday to Wednesday and interval to "ww" (week), then DateDiff returns the number of Wednesdays between two dates:

Const WEDNESDAY = 4

Dim NumWed As Integer

NumWed = DateDiff("ww", "11/1", "11/30", WEDNESDAY)

The following table lists the valid time periods and their interval values.

Time period
interval
Year
yyyy
Quarter
q
Month
m
Day of year
y
Day
d
Weekday
w
Week
ww
Hour
h
Minute
n
Second
s

If you want to know the number of days between date1 and date2, you can use either y (day of year) or d (day).

When you omit the year, DateDiff uses the current year. If a date is enclosed by number signs (#), the current year becomes a permanent part of that date. If you enclose the date in double quotation marks (""), however, the current year is inserted in your code each time it is executed. This makes it possible to write code that can be used over multiple years.

When interval is w (weekday), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date1 but not date2. If interval is ww (week), however, the DateDiff function returns the number of calendar weeks between the two dates; it counts the number of Sundays between date1 and date2. DateDiff counts date1 if it falls on a Sunday; but it doesn't count date2, even if it does fall on a Sunday.

If date1 refers to a later point in time than date2, the DateDiff function returns a negative number.

Example

The following example calculates the number of days between today and New Year's Eve:

NumDays = DateDiff("y", Now(), "31-Dec")