Visual Basic Language Reference  

TimeSerial Function

Returns a Date value representing a specified hour, minute, and second, with the date information set relative to January 1 of the year 1.

Public Function TimeSerial( _
   ByVal Hour As Integer, _
   ByVal Minute As Integer, _
   ByVal Second As Integer _
) As DateTime

Parameters

Hour
Required. Integer expression from 0 through 23. However, values outside this range are also accepted.
Minute
Required. Integer expression from 0 through 59. However, values outside this range are also accepted. The value of Minute is added to the calculated hour, so a negative value specifies minutes before that hour.
Second
Required. Integer expression from 0 through 59. However, values outside this range are also accepted. The value of Second is added to the calculated minute, so a negative value specifies seconds before that minute.

Exceptions/Errors

Exception type Error number Condition
ArgumentException 5 Argument is outside the range -2,147,483,648 through 2,147,483,647
ArgumentOutOfRangeException 9 Calculated time is less than negative 24 hours.

Remarks

The following example demonstrates negative, zero, and positive argument values. Here, the TimeSerial function returns a time representing 15 minutes before three hours before noon, or 8:45:00 A.M.:

Dim AlarmTime As Date = TimeSerial(12 - 3, -15, 0)

If either Minute or Second exceeds its normal range, it is applied to the next larger unit as appropriate. For example, if you specify 75 minutes, it is evaluated as one hour and 15 minutes. If any argument is outside the range -2,147,483,648 through 2,147,483,647, an ArgumentException error occurs.

TimeSerial reduces the total seconds modulo 86,400, which is the number of seconds in a day. Therefore, the returned time is always between 00:00:00 and 23:59:59.

The Date data type includes date components. TimeSerial sets all of these to 1, so the returned value represents the first day of the year 1. However, if the values of the arguments cause the calculated time to exceed 24 hours, the day is incremented as necessary. In the following example, the values of Hour and Minute result in a combined time of more than 24 hours.

MsgBox(TimeSerial(23, 75, 0))  ' Displays "1/2/0001 12:15:00 AM"

If the values of the arguments result in a negative calculated time, the date information is set to 1/1/0001 and the time information is adjusted to be between 00:00:00 and 23:59:59. However, if the calculated time is less than negative 24 hours, an ArgumentOutOfRangeException error occurs.

Since every Date value is supported by a DateTime structure, its methods give you additional options in assembling a Date value. For example, you can employ one of the overloaded DateTime constructors to populate a Date variable using the desired combination of components. The following example sets NewDateTime to May 6, 1978 at one tenth of a second before 8:30 in the morning:

Dim NewDateTime As Date = New Date(1978, 5, 6, 8, 29, 59, 900)

Example

This example uses the TimeSerial function to return a time for the specified hour, minute, and second.

Dim MyTime As Date
MyTime = TimeSerial(16, 35, 17)   ' Return the time in a Date data type.

See Also

DateSerial Function | DateValue Function | Hour Function | Minute Function | Now Property | Second Function | TimeValue Function | System Namespace | DateTime Structure | ArgumentOutOfRangeException Class | ArgumentOutOfRangeException