DateSerial Function

Description

Returns a Variant (Date) for a specified year, month, and day.

Syntax

DateSerial(year, month, day)

The DateSerial function syntax has these named arguments

Part

Description

year

Required; Integer. Number between 100 and 9999, inclusive, or a numeric expression.

month

Required; Integer. Any numeric expression.

day

Required; Integer. Any numeric expression.


Remarks

To specify a date, such as December 31, 1991, the range of numbers for each DateSerial argument should be in the accepted range for the unit; that is, 1 – 31 for days and 1 – 12 for months. However, you can also specify relative dates for each argument using any numeric expression that represents some number of days, months, or years before or after a certain date.

The following example uses numeric expressions instead of absolute date numbers. Here the DateSerial function returns a date that is the day before the first day
(1 - 1), two months before August (8 - 2), 10 years before 1990 (1990 - 10); in other words, May 31, 1980.

DateSerial(1990 - 10, 8 - 2, 1 - 1)
For the year argument, values between 0 and 99, inclusive, are interpreted as the years 1900 – 1999. For all other year arguments, use a four-digit year (for example, 1800).

When any argument exceeds the accepted range for that argument, it increments to the next larger unit as appropriate. For example, if you specify 35 days, it is evaluated as one month and some number of days, depending on where in the year it is applied. If any single argument is outside the range –32,768 to 32,767, an error occurs. If the date specified by the three arguments falls outside the acceptable range of dates, an error occurs.

See Also

Date function, Date statement, DateValue function, Day function, Month function, Now function, TimeSerial function, TimeValue function, Weekday function, Year function.

Example

This example uses the DateSerial function to return the date for the specified year, month, and day.

Dim MyDate
' MyDate contains the date for February 12, 1969.
MyDate = DateSerial(1969, 2, 12)    ' Return a date.
Example (Microsoft Access)

The following example uses the DateSerial function together with the Year, Month, and Day functions to return the number of days in a month. You can pass either a date or a string to the DaysInMonth function.

Function DaysInMonth(dteInput As Date) As Integer
    Dim intDays As Integer

    ' Add one month, subtract dates to find difference.
    intDays = DateSerial(Year(dteInput), Month(dteInput) + 1, Day(dteInput)) _
        - DateSerial(Year(dteInput), Month(dteInput), Day(dteInput))
    DaysInMonth = intDays
    Debug.Print intDays
End Function
The following Sub procedure shows several different ways that you might call the DaysInMonth function.

Sub CallDaysInMonth()
    Dim intDays As Integer
    intDays = DaysInMonth(#4/1/96#)
    intDays = DaysInMonth("4-1-96")
    intDays = DaysInMonth("April 1, 1996")
End Sub