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.
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. or string expression that can represent a date and time from January 1, 100 through December 31, 9999, where January 1, 1900 is 2.
If dateexpression is Null, this function returns a Null.
Time, Time$ Functions
Time, Time$ Statements
Hour Function Example
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.
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
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.