VBA Functions: Hour, Minute, and Second

The VBA functions Hour, Minute, and Second return an integer corresponding to the hour, minute, or second of the specified date or time.

Usage:

Hour(date_time)

Minute(date_time)

Second(date_time)


Example of Usage

Using the Hour, Minute, and Second functions to retrieve corresponding information from a time:

Sub example()

    myTime = #6:36:45 PM#

    MsgBox Hour(myTime) 'Returns: 18
    MsgBox Minute(myTime) 'Returns: 36
    MsgBox Second(myTime) 'Returns: 45

End Sub

Or based on a date and time (for the same result):

Sub example()

    myDateTime = #10/31/2024 6:36:45 PM#

    MsgBox Hour(myDateTime) 'Returns: 18
    MsgBox Minute(myDateTime) 'Returns: 36
    MsgBox Second(myDateTime) 'Returns: 45

End Sub

These functions also accept date and time strings:

Sub example()

    myDateTime = "10/31/2024 18:36:45"

    MsgBox Hour(myDateTime) 'Returns: 18
    MsgBox Minute(myDateTime) 'Returns: 36
    MsgBox Second(myDateTime) 'Returns: 45

End Sub