Taking the Time into Account in a Formula

by Allen Wyatt
(last updated January 11, 2014)

8

Ken would like to display the contents of a cell based on the time of day. For instance, if cell C1 contains the NOW function (for the time of day), in another cell he would like a formula that checks that time. If it is before 5:00 pm, then the formula displays the contents of cell E1. If the time is after 5:00 pm, then the formula displays the contents of cell F1. Ken is not sure how to work out the IF statement so that it checks the time properly.

The key to accomplishing this task is remembering how Excel stores times and dates internally. When you use the NOW function, it returns a serial number that represents the current date and time. Everything in that serial number to the left of the decimal point is the date, and everything to the right is the time. Get rid of the date portion before doing your comparison, and you are then comparing apples to apples (so to speak).

One way to get rid of the date portion of the serial number is in this manner:

=IF(C1-INT(C1)<TIME(17,0,0),E1,F1)

The TIME function returns only the time portion of a date/time serial number for whatever hours, minutes, and seconds you specify in the parameters. Since you want 5:00 pm, then only the hours portion needs to be set to 17.

Another way to strip out the date portion of the serial number is to use the MOD function, in this manner:

=IF(MOD(C1,1)<TIME(17,0,0),E1,F1)

The function returns the remainder after dividing the value in C1 by 1. The remainder, in this case, is everything to the right of the decimal point—the time.

Perhaps the easiest way to do the comparison, however, is to skip any math on the value in C1 altogether. Instead, utilize the HOUR function, in this manner:

=IF(HOUR(C1)<17,E1,F1)

The HOUR function examines the date/time serial number in cell C1 and returns a value of 0 through 23, depending on the hour of the day represented by that serial number. Compare this to 17 (the value of the 5:00 pm hour) and your formula can display the desired value based on the time of day.

It is important to remember that the NOW function (on which your formula hinges) is updated only when the worksheet is updated. This means that if your worksheet is updated at 4:55 pm and then not updated again until 5:10 pm, then during that 15 minutes the formula will return the "before 5:00 pm" value because it doesn't yet know it is after 5:00 pm until the recalculation.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12889) applies to Microsoft Excel 2007, 2010, and 2013.

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 the Status Bar

When developing a macro, you may want to display on the status bar what the macro is doing. Here's how to use this important ...

Discover More

Creating an Organization Chart

Graphics are often added to worksheets to make it easier to understand the data contained in the worksheet. Sometimes, ...

Discover More

Macros Run Slower in Newer Excel?

If you run a macro you used in an older version of Excel on a newer system, it may seem like the macro runs slower. Here are ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Counting Times within a Range

Excel allows you to easily store dates and times in your worksheets. If you have a range of cells that contain times and you ...

Discover More

Using Excel for Timing

Excel allows you to store times in a worksheet. If you want to use Excel to time certain events, there are a couple of ways ...

Discover More

Displaying a Result as Minutes and Seconds

When you use a formula to come up with a result that you want displayed as a time, it can be tricky figuring out how to get ...

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}] 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 nine more than 8?

2014-01-21 10:44:00

Laura

When or how often does a worksheet get updated? Only when the 'save' button is hit? Is there an auto-update to help me keep it updated?


2014-01-18 15:51:03

Erik Oberg

Let me add that, in my opinion, Allen's first proposal is the best:
=IF(C1-INT(C1)<TIME(17,0,0),E1,F1)
It is easy to understand, and can capture ANY time easily.
There are often many "right" ways, and one of the reasons I read ExcelTips is to learn ways I had not considered. In this case, I learned about the TIME function. Thanks, Allen!


2014-01-18 14:44:00

Erik Oberg

Bryan, you are correct. And Aldo, your formula has a second error as well: 1730/2400 = 5:18 pm, because there are only 60 minutes in 1 hour. I proposed using =IF(C1-TRUNC(C1) < (17+30/60)/24,E1,F1) when I submitted my answer, but that did not make it into the published newsletter. Of course (17.5/24) = (17+30/60)/24, but I would argue dividing the minutes by 60 inside the formula is better - easier for others to understand.


2014-01-14 11:27:37

Bryan

Micky, Aldo, don't you need to take the integer off of the value in C1?


2014-01-13 22:43:24

Aldo

And if you need minutes like 5:30 pm use...

=IF(C1<1730/2400,E1,F1)


2014-01-11 14:05:23

Juan

Excellent tio, it's very useful to understand how Excel works with times and dates internally


2014-01-11 12:29:28

Michael (Micky) Avidan

Another alternative: =IF(C1<17/24,E1,F1)
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2014)
ISRAEL


2014-01-11 11:03:11

KenBachelor

Thank you for your help, =IF(HOUR(C1)<17,E1,F1) seems to work perfectly.


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.