See Also Example
Returns the time represented by a String argument.
Syntax
TimeValue(stringexpression)
Remarks
The argument stringexpression is a String representing a time from 0:00:00 (12:00:00 A.M.) through 23:59:59 (11:59:59 P.M.). You can enter valid times using a 12- or 24-hour clock. For example, "2:24PM" and "14:24" are both valid time arguments.
If stringexpression contains date information, TimeValue doesn't return it. However, if stringexpression includes invalid date information, an error occurs.
The TimeValue 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.
DateSerial Function
DateValue Function
Hour Function
Minute Function
Now Function
Second Function
TimeSerial Function
In this example, the TimeValue function creates a Variant of VarType 7 (Date) for midnight. The Hour, Minute, and Second functions determine the hour, minute, and second values so the difference can be calculated. 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 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.
End If
If MinuteDiff = 60 Then
HourDiff = HourDiff + 1 ' Add 1 to hour.
MinuteDiff = 0 ' Zero minutes.
End If
TotalMinDiff = (HourDiff * 60) + MinuteDiff ' Get totals.
TotalSecDiff = (TotalMinDiff * 60) + 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."
MsgBox Msg ' Display message.
End Sub