DateAndTime.TimeSerial(Int32, Int32, Int32) Method

Definition

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:
 static DateTime TimeSerial(int Hour, int Minute, int Second);
public static DateTime TimeSerial (int Hour, int Minute, int Second);
static member TimeSerial : int * int * int -> DateTime
Public Function TimeSerial (Hour As Integer, Minute As Integer, Second As Integer) As DateTime

Parameters

Hour
Int32

Required. Integer expression from 0 through 23. However, values outside this range are also accepted.

Minute
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
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.

Returns

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

Exceptions

An argument is outside the range -2,147,483,648 through 2,147,483,647

Calculated time is less than negative 24 hours.

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)

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)  

Applies to

See also