Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Determining the Hour of the Day.

Determining the Hour of the Day

Written by Allen Wyatt (last updated December 16, 2021)
This tip applies to Excel 2007, 2010, and 2013


1

If you are writing macros for Excel, you may have a need to determine the hour represented by a particular date and time value. For instance, you might want to know the hour of the day in which the macro is running. You can ascertain this information by using the HOUR function, as follows:

iThisHour = Hour(Now())

When executed, iThisHour will be equal to the current hour number, which ranges from 0 to 23. Notice that this example uses the Now() function. If you want to determine the hour number for a different date and time value, simply substitute that value in place of the Now() function.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9009) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Determining the Hour of the Day.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Using Chapter Numbers with Page Numbers

Do you need to add page numbers that include, as well, a chapter number? It's relatively easy to do, as described in this ...

Discover More

Nifty Zooming

If you are using a mouse that has a center wheel, you can use the wheel to zoom in and out of your work. This tip shows ...

Discover More

Heavy-Duty Footnotes

Word allows you to add footnotes to a document, but they are rather straightforward and simple in their application. If ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Understanding the Select Case Structure

One of the powerful programming structures available in VBA is the Select Case structure. This tip explains how you can ...

Discover More

Stepping Through a Non-Contiguous Range of Cells

Using macros to step through each cell in a selection is a common occurrence. What if that selected range is made up of ...

Discover More

Page Numbers in VBA

When you print a larger worksheet, Excel breaks the printout across several pages. You may want to know, before you ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 8?

2013-10-15 10:07:47

Bryan

In VBA you don't need to include the parentheses if there are no arguments, so Hour(Now) is the same as Hour(Now()). There's no harm in leaving them, other than you might be tempted to put in an argument where there is none.

In contrast, in Excel you must use the parentheses, as this is how Excel knows you are referencing a function. =HOUR(NOW()) will give you the correct result, whereas =HOUR(NOW) will display the #NAME? Error.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.