DateSerial Function

See AlsoIKCY7W              Example16TG9UI>Low

Returns the date serial for a specific year, month, and day.

Syntax

DateSerial(year,month,day)

Remarks

The DateSerial function has these parts:

Part               Description

 

year               A number between 100 and 9999, inclusive, or a numeric expression71RISN.

month            A number between 1 and 12, inclusive, or a numeric expression.

day                A number between 1 and 31, inclusive, or a numeric expression.

To express a specific date, such as December 31, 1991, the range of numbers for each DateSerial argument should conform to the accepted range of values for the unit.  These values are 1 through 31 for days and 1 through 12 for months.  You also can specify relative dates for each argument by using a numeric expression representing the number of days, months, or years before or after a certain date.  The following example uses expressions instead of absolute date numbers.  The DateSerial function returns a date that is the day before the first day (1 - 1) of two months before August (8 - 2) of 10 years before 1990 (1990 - 10)in other words, May, 31, 1980.

   DateSerial(1990 - 10, 8 - 2, 1 - 1)

 

For the argument year, values between 0 and 99, inclusive, are interpreted as the years 1900-1999.  For all other year arguments, use the complete four-digit year (for example, 1800).

The DateSerial function returns a Variant8PHEAW3 of VarType7A68ZTZ 7 (Date) containing a date that is stored internally as a double-precision number.  This number represents a date from January 1, 100 through December 31, 9999, where January 1, 1900 is 2.  Negative numbers represent dates prior to December 30, 1899.

If the date specified by the three arguments, either directly or by expression, falls outside the acceptable range of dates, an error occurs.


See Also

DateValue FunctionEZ3HZJ

Day FunctionLANDAY

Month Function3NARHG

Now FunctionLANNOW

TimeSerial FunctionA54OFU

TimeValue Function1SIT1R5

Weekday FunctionM393JM

Year FunctionGUFX5G


DateSerial Function Example

In this example, the DateSerial function returns a Variant (VarType 7) containing a date created from the integer arguments.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

 

Sub Form_Click ()

   Dim Msg, RelVal, UserDate, Verb             ' Declare variables.

   Dim Flag, DD, MM, YY, WhatDay

   Flag = False

   Msg = "Enter a date in form mm/dd/yyyy"

   Do

      UserDate = InputBox(Msg)                 ' Get user input.

      If Len(UserDate) <> 10 Then UserDate = Format(Now, "mm/dd/yyyy")

      MM = Left(UserDate, 2)                   ' Get month number.

      If MM >= 1 And MM <= 12 Then Flag = True Else Flag = False

      DD = Mid(UserDate, 4, 2)                 ' Get day number.

      If DD >= 1 And DD <= 31 Then Flag = True Else Flag = False

      YY = Right(UserDate, 4)                  ' Get year number.

      If YY >= 1753 And YY <= 2078 Then Flag = True Else Flag = False

   Loop Until Flag = True

   RelVal = DateSerial(YY, MM, DD)             ' Get date serial.

   Select Case RelVal                          ' Use correct verb.

      Case Is < Int(Now): Verb = " was a "

      Case Is > Int(Now): Verb = " will be a "

      Case Else: Verb = " is a "

   End Select

   WhatDay = Format(RelVal, "dddd")            ' Determine day.

   Msg = UserDate & Verb & WhatDay & "."

   MsgBox Msg                                  ' Display message.

End Sub