DateDiff Function


DateDiff Function

Class

Microsoft.VisualBasic.DateAndTime

Syntax

     Dim result As Long = DateDiff(interval, date1, date2[, _         dayOfWeek[, weekOfYear]]) 


interval (required; String or DateInterval enumeration)

A string expression or Microsoft.VisualBasic.DateInterval enumeration item indicating which part of the date to report in the return value. The following table lists both the string and enumeration choices.

String

DateInterval

Description

d

Day

The difference in days

y

DayOfYear

The difference in days

h

Hour

The difference in hours

n

Minute

The difference in minutes

m

Month

The difference in months

q

Quarter

The difference in quarters

s

Second

The difference in seconds

w

Weekday

The difference in weeks, based on counting individual days

ww

WeekOfYear

The difference in weeks, based on full calendar weeks

yyyy

Year

The difference in years



date1 (required; Date)

The first date from which to calculate the difference.


date2 (required; Date)

The second date from which to calculate the difference. To return a positive result, this date should occur after date1.


dayOfWeek (optional; FirstDayOfWeek enumeration)

Indicates the first day of the week. One of the following Microsoft.VisualBasic.FirstDayOfWeek enumeration members: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or System (to use the regional default). If omitted, Sunday is used.


weekOfYear (optional; FirstWeekOfYear enumeration)

Indicates which week counts as the first week of a given year. One of the following Microsoft.VisualBasic.FirstWeekOfYear enumeration members.

Value

Description

System

Uses the system-defined value

Jan1

Uses the week in which January 1 appears

FirstFourDays

Uses the first week of the year that has at least four days in it

FirstFullWeek

Uses the first week of the year that has a full seven days in it


If omitted, Jan1 is used.

Description

The DateDiff function calculates the number of time intervals between two dates. For example, you can use the function to determine how many days there are between January 1, 1980, and May 31, 1998.

Usage at a Glance

  • To calculate the number of days between date1 and date2, you can use the DateInterval constants Day or DayOfYear or the string literals "d" or "y."

  • When interval is Weekday or "w," DateDiff returns the number of weeks between the two dates based on the number of days between the two dates. When interval is Week or "ww," DateDiff returns the number of weeks by first considering the full weeks in which date1 and date2 appear and then counting based on the first day of each of these weeks. The dayOfWeek parameter is significant in this calculation.

  • In the calculation, the interval is determined by subtracting date1 from date2. If date1 appears before date2 in chronological order, the return value will be positive; if date1 appears after date2 in chronological order, the return value will be negative.

  • The expression DateDiff("yyyy", #12/31/2005#, #1/2/2006#) returns 1, even though only two days have elapsed. Similar results occur for related differences in other intervals.

  • DateDiff considers the four quarters of the year to be January 1 to March 31, April 1 to June 30, July 1 to September 30, and October 1 to December 31.

Version Differences

  • VB 6 lacks the DateInterval enumeration and therefore only accepts a string for the interval argument.

  • VB 6 supports a number of constants beginning with vb... (such as vbSunday) as values for the dayOfWeek and weekOfYear arguments. While these are still supported in .NET, the new FirstDayOfWeek and FirstWeekOfYear enumerations are preferred.

See Also

DateAdd Function




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net