Name

DateDiff Function

Class

Microsoft.VisualBasic.DateAndTime

Syntax

DateDiff(interval, date1, date2[, dayofweek[, weekofyear]])
interval (required; String or DateInterval enum)

A String expression (see the first item in Section ) or a member of the DateInterval enumeration (see the second item in Section ) that specifies the units of time used to express the difference between date1 and date2

date1, date2 (required; Date or a literal date)

The starting and ending dates, whose difference is computed as date2 - date1

dayofweek (optional; FirstDayOfWeek enum)

A member of the FirstDayOfWeek enum

weekofyear (optional; FirstWeekOfYear enum)

A member of the FirstWeekOfYear enum

Return Value

A Long specifying the number of time intervals between the two dates

Description

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

Rules at a Glance

  • interval can be one of the following literal strings:

String

Description

yyyy

Year

q

Quarter

m

Month

y

Day of year

d

Day

w

Weekday

ww

Week

h

Hour

n

Minute

s

Second

  • interval can also be a member of the DateInterval enum:

    Enum DateInterval
       Day
       DayOfYear
       Hour
       Minute
       Month
       Quarter
       Second
       Week
       Weekday
       WeekOfYear
    End Enum
  • To calculate the number of days between date1 and date2, you can use either of the DateInterval constants, DayOfYear or Day, or the string literals "y" or "d".

  • When interval is Weekday or "w", DateDiff

Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.