See Also Example
Returns an integer between 0 and 23, inclusive, that represents the hour of the day corresponding to the time provided as an argument.
Syntax
Hour(dateexpression)
Remarks
The dateexpression part of the syntax is any numeric expression Numbers to the left of the decimal point in dateexpression represent the date; numbers to the right represent the time. Negative numbers represent dates prior to December 30, 1899.
If dateexpression is Null
Day Function
Minute Function
Month Function
Now Function
Second Function
Time, Time$ Functions
Time, Time$ Statements
Weekday Function
Year 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