Partager via


DateAndTime.TimeSerial Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

Syntax

'Declaration
Public Shared Function TimeSerial ( _
    Hour As Integer, _
    Minute As Integer, _
    Second As Integer _
) As DateTime
public static DateTime TimeSerial(
    int Hour,
    int Minute,
    int Second
)

Parameters

  • Hour
    Type: System.Int32
    Required. Integer expression from 0 through 23. However, values outside this range are also accepted.
  • Minute
    Type: System.Int32
    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
    Type: System.Int32
    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.

Return Value

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

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)

Examples

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)

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.