Hour Function

See Also7Z0951              Example3IAYFW>Low

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 expression71RISN or string expression1330R89 that can represent a date and time from January 1, 100 through December 31, 9999, where January 1, 1900 is 2.  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 Null1DDW7C0, this function returns a Null.


See Also

Day FunctionLANDAY

Minute Function5OWKHCJ

Month Function3NARHG

Now FunctionLANNOW

Second FunctionC46EYJ

Time, Time$ FunctionsQ3GPPU

Time, Time$ StatementsQ3GPQ7

Weekday FunctionM393JM

Year FunctionGUFX5G


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.

   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