Bagikan melalui


TimeSerial Function (Visual Basic)

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

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.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

Remarks

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

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.

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)) 
' The preceding statement 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 System.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

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

Dim thisTime As Date
thisTime = TimeSerial(16, 35, 17)

Requirements

Namespace: Microsoft.VisualBasic

Module: DateAndTime

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Reference

DateSerial Function (Visual Basic)

DateValue Function (Visual Basic)

Hour Function (Visual Basic)

Minute Function

Now Property

Second Function (Visual Basic)

TimeValue Function (Visual Basic)