See Also Example
Returns the time serial for a specific hour, minute, and second.
TimeSerial(hour, minute, second)
The TimeSerial function has these parts:
hour An hour between 0 (12:00 A.M.) and 23 (11:00 P.M.), inclusive, or a numeric expression .
minute A minute between 0 and 59, inclusive, or a numeric expression.
second A second between 0 and 59, inclusive, or a numeric expression.
To express a specific time, such as 11:59:59, the range of numbers for each TimeSerial argument should conform to the accepted range of values for the unit. These values are 0 through 23 for hours and 0 through 59 for minutes and seconds. You also can specify relative times for each argument by using a numeric expression representing the number of hours, minutes, or seconds before or after a certain time. The following example uses expressions instead of absolute time numbers. The TimeSerial function returns a time for 15 minutes before (0 - 15) six hours before noon (12 - 6), or 5:45:00 A.M.
TimeSerial(12 - 6, 0 - 15, 0)
The TimeSerial function returns a Variant This number represents a time between 0:00:00 and 23:59:59, or 12:00:00 A.M. and 11:59:59 P.M., inclusive. of VarType 7 (Date) containing a time that is stored internally as a double-precision fractional number between 0 and .99999.
If the time specified by the three arguments, either directly or by expression, falls outside the acceptable range of times, an error occurs.
TimeSerial Function Example
In this example, the TimeSerial function creates a time serial from a number of hours, minutes, and seconds. 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 HourDiff, MinuteDiff, Msg, SecondDiff ' Declare variables
Dim Instant, Midnight, TotalMinDiff, TotalSecDiff
Midnight = TimeValue("23:59:59")
Instant = Now ' Get current time.
HourDiff = Hour(Midnight) - Hour(Instant) ' Get the differences.
MinuteDiff = Minute(Midnight) - Minute(Instant)
SecondDiff = Second(Midnight) - Second(Instant) + 1
If SecondDiff = 60 Then
MinuteDiff = MinuteDiff + 1 ' Add 1 to minute.
SecondDiff = 0 ' Zero seconds.
If MinuteDiff = 60 Then
HourDiff = HourDiff + 1 ' Add 1 to hour.
MinuteDiff = 0 ' Zero minutes.
TotalMinDiff = (HourDiff * 60) + MinuteDiff ' Get totals.
TotalSecDiff = (TotalMinDiff * 60) + SecondDiff
TotalDiff = TimeSerial(HourDiff, MinuteDiff, SecondDiff)
Msg = "There are a total of " & Format(TotalSecDiff, "#,##0")
Msg = Msg & " seconds until midnight. That translates to "
Msg = Msg & HourDiff & " hours, " & MinuteDiff
Msg = Msg & " minutes, and " & SecondDiff & " seconds."
Msg = Msg & " Expressed in standard time notation, it becomes "
Msg = Msg & Format(TotalDiff, "hh:mm:ss") & "."
MsgBox Msg ' Display message.