How to use the HOUR function
What is the HOUR function?
The HOUR function returns an integer representing the hour of an Excel time value. The returning number is ranging from 0 (12:00 A.M.) to 23 (11:00 P.M.).
Table of Contents
1. Introduction
1. What is an integer?
An integer is a whole number that can be positive, negative, or zero, but not a fraction or decimal. Excel can't calculate the hour based on negative integers.
What is an Excel time value?
Excel time is actually a decimal number ranging between 0 and 1 in Excel and then formatted as time.
For example, 12:00 PM is represented as 0.5 because it is half of a day, you can verify this by typing 12:00 PM in a cell and then change the cell formatting to general. This will show the value as Excel interprets it.
How does Excel recognize time values?
Excel recognizes certain text strings like "6:45 PM" as valid time values. A recognized time value is right aligned in the cell just like a regular number, shown in the image below in cell B2.
A time number that is not recognized is left aligned which is demonstrated in cell B4 in the image above. This visual feedback lets you easily spot values that need closer inspection.
What is an hour in an Excel time value?
1/24 or approx. 0.04166667 represents one hour in Excel time value. Excel uses a number 0 <= x <= 1 in decimal form to represent time in a worksheet. 0 is zero hours and 1 is 24 hours. To get one hour we must divide 1 by 24 and we get 1/24.
Why is 1 equal to 24 hours?
This has to do how Excel handles dates. Each date is represented by an integer and one day is 1 in Excel.
Dates are stored numerically but formatted to display in human-readable date/time formats, this enables Excel to do work with dates in calculations.
For example, dates are stored as sequential serial numbers with 1 being January 1, 1900 by default. The integer part (whole number) represents the date the decimal part represents the time.
This allows dates to easily be formatted to display in many date/time formats like mm/dd/yyyy, dd/mm/yyyy and so on and still be part of calculations as long as the date is stored numerically in a cell.
You can try this yourself, type 10000 in a cell, press CTRL + 1 and change the cell's formatting to date, press with left mouse button on OK. The cell now shows 5/18/1927.
What is the decimal form?
The decimal form or decimal number system represents values using digits 0-9 and a decimal point. Each digit position represents a power of 10 - units, tens, hundreds, etc. The decimal point separates the integer part from the fractional part. Decimal values allow representation of fractional numbers in a standard notation.
Where is AM/PM used?
AM/PM is used in the United States, Canada, Mexico, Australia, United Kingdom and a few other countries. AM refers to times from midnight to noon. PM refers to times from noon to midnight.
Where is the 24 hour clock used?
The 24 hour clock is used in continental Europe including Scandinavia, China, India and most countries except those above. Used in formal documents like schedules, timetables, business contracts. Used by scientists, aviation, hospitals, transportation services, military. Avoids AM/PM ambiguity and potential confusion.
AM/PM | 24 hour clock |
12:00 AM | 0:00 |
1:00 AM | 1:00 |
2:00 AM | 2:00 |
3:00 AM | 3:00 |
4:00 AM | 4:00 |
5:00 AM | 5:00 |
6:00 AM | 6:00 |
7:00 AM | 7:00 |
8:00 AM | 8:00 |
9:00 AM | 9:00 |
10:00 AM | 10:00 |
11:00 AM | 11:00 |
12:00 PM | 12:00 |
1:00 PM | 13:00 |
2:00 PM | 14:00 |
3:00 PM | 15:00 |
4:00 PM | 16:00 |
5:00 PM | 17:00 |
6:00 PM | 18:00 |
7:00 PM | 19:00 |
8:00 PM | 20:00 |
9:00 PM | 21:00 |
10:00 PM | 22:00 |
11:00 PM | 23:00 |
12:00 AM | 0:00 |
2. HOUR function Syntax
HOUR(serial_number)
3. HOUR function Arguments
serial_number | Required. An Excel time value that you want to calculate the hour of. |
4. HOUR function example
This example demonstrates the HOUR function in cell C3, it extracts the hour from the Excel time value specified in cell B3: "1:34:22 PM"
Formula in cell C3:
1:00 PM represents the 13th hour.
5. HOUR function not working
The HOUR function returns #NUM! error if the decimal is a negative value.
The HOUR function returns #VALUE! error if the Excel time value is invalid.
6. HOUR function alternative - cell formatting
You also have the option to format a cell containing an Excel time value so it shows only the hour part. Here is how:
- Select the cell.
- Press CTRL + 1 to open the "Format Cells" dialog box.
- Select category: Custom
- Create a new type: hh
- Press the "OK" button.
The image above shows cell B3 containing the following time value "1:34:22 PM", cell C3 references that value. This means that cell C3 contains the same value, however, cell formatting is applied which makes it show only the hour part.
7. Count complete hours between two time values
The formula in cell D5 calculates the number of complete hours between the time entries in cell B5 and C5.
Formula in cell D5:
Note that this formula works only with time entries, not date and time entries. It doesn't matter if the start time is past the end time, the formula will regardless return a positive hour value.
Explaining formula in cell D5
Step 1 - Subtract time entries
C5-B5
becomes
6:00:00 AM - 12:00:00 AM
Time entries in excel are actually a decimal value between 0 (zero) and 1. One hour is 1/24, six hours is 6/24 and so on.
0.25 - 0 equals 0.25
Step 2 - Remove sign
The ABS function removes the minus sign if it exists, the HOUR function can't calculate negative values.
ABS(C5-B5)
becomes
ABS(0.25) and returns 0.25.
Step 3 - Convert decimal to hour(s)
The HOUR function calculates hours based on a decimal value.
HOUR(ABS(C5-B5))
becomes
HOUR(0.25)
and returns 6.
The image above demonstrates a formula the returns negative hours if the start time is later than the end time.
Formula in cell D5:
Explaining formula in cell D5
This part of the formula HOUR(ABS(C5-B5)) is explained above.
Step 1 - Calculate if end date is smaller than the start date
C5<B5
becomes
0.25<0
and returns FALSE.
Step 2 - Return -1 if TRUE and 1 if FALSE
IF(C5<B5,-1,1)
becomes
IF(0.25<0,-1,1)
becomes
IF(FALSE,-1,1) and returns 1.
Step 3 - Multiply with result
HOUR(ABS(C5-B5))*IF(C5<B5,-1,1)
becomes
6*IF(C5<B5,-1,1)
becomes
6*1 and returns 6 in cell D5.
8. Count complete hours between two date and time values
The picture above shows a formula that calculates complete hours between date and time entries.
Formula in cell D5:
If you want negative hours then remove the ABS function:
The TEXT function formats the result of C5-B5 into complete hours.
'HOUR' function examples
Functions in 'Date and Time' category
The HOUR function function is one of 22 functions in the 'Date and Time' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form