If formula excel for time

The IF function is one of the most useful Excel functions. It is used to test a condition and return one value if the condition is TRUE and another if it is FALSE.

One of the most common applications of the IF function involves the comparison of values.

These values can be numbers, text, or even dates. However, using the IF statement with date values is not as intuitive as it may seem.

In this tutorial, I will demonstrate some ways in which you can use the IF function with date values.

Syntax and Usage of the IF Function in Excel

The syntax for the IF function is as follows:

IF(logical_test, [value_if_true], [value_if_false])

Here,

  • logical_test is the condition or criteria that you want the IF function to test. The result of this parameter is either TRUE or FALSE
  • value_if_true is the value that you want the IF function to return if the logical_test evaluates to TRUE
  • value_if_false is the value that you want the IF function to return if the logical_test evaluates to FALSE

For example, say you want to write a statement that will return the value “yes” if the value in cell reference A2 is equal to 10, and “no” if it’s anything but 10.

You can then use the following IF function for this scenario:

=IF(A2=10,"yes","no")
A Simple IF formula

Comparing Dates in Excel (Using Operators)

Unlike numbers and strings, comparison operators, when used with dates, have a slightly different meaning.

Here are some of the comparison operators that you can use when comparing dates, along with what they mean:

Operator What it Means When Using with Dates
< Before the given date
= Same as the date with which we are comparing
> After the given date
<= Same as or before the given date
>= Same as or after the given date

It may look like IF formulas for dates are the same as IF functions for numeric or text values, since they use the same comparison operators.

However, it’s not as simple as that.

Unfortunately, unlike other Excel functions, the IF function cannot recognize dates.

It interprets them as regular text values.

So you cannot use a logical test such as “>05/07/2021” in your IF function, as it will simply see the value “05/07/2021” as text.

Here are a few ways in which you can incorporate date values into your IF function’s logical_test parameter.

Using the IF Function with DATEVALUE Function

If you want to use a date in your IF function’s logical test, you can wrap the date in the DATEVALUE function.

This function converts a date in text format to a serial number that Excel can recognize as a date.

If you put a date within quotes, it is essentially a text or string value.

When you pass this as a parameter to the DATEVALUE function, it takes a look at the text inside the double quotes, identifies it as a date and then converts it to an actual Excel date value.

Let us say you have a date in cell A2, and you want cell B2 to display the value “done” if the date comes before or on the same date as “05/07/2021” and display “not done” otherwise.

You can use the IF function along with DATEVALUE in cell B2 as follows:

=IF(A2<DATEVALUE("05/07/2021"),"done","not done")

Here’s a screenshot to illustrate the effect of the above formula:

IF Formula to check done or not

Using the IF Function with the TODAY Function

If you want to compare a date with the current date, you can use the IF function with the TODAY function in the logical test.

Let’s say you have a date in cell A2 and you want cell B2 to display the value “done” if it is a date before today’s date.

If not, you want let’s say you want to display the value “not done”. You can use the IF function along with the TODAY function in cell B2 as follows:

=IF(A2<TODAY(),"done","not done")

Here’s a screenshot to illustrate the effect of the above formula (assuming the current date is 05/08/2021):

IF Formula to check whether date is before today

Using the IF Function with Future or Past Dates

An interesting thing about dates in Excel is that you can perform addition and subtraction operations with them too.

This is because dates are basically stored in Excel as serial numbers, starting from the date Jan 1, 1900.

Each day after that is represented by one whole number.

So, the serial number 2 corresponds to Jan 2, 1900, and so on.

This means that adding n number of days to a date is equivalent to adding the value n to the serial number that the date represents.

If TODAY() is 05/07/2021, then TODAY()+5 is five days after today, or 05/12/2021. Similarly, TODAY()-3 is three days before today or 05/04/2021.

Let’s say you have a date in cell A2 and you want cell B2 to mark it as “within range” if it is within 15 days from the current date.

If not, you want to show “out of range”. You can use the IF function along with the TODAY function in cell B2 as follows:

=IF(A2<TODAY()+15,"within range","out of range")

Here’s a screenshot to illustrate the effect of the above formula (assuming the current date is 05/08/2021):

IF formula to check whether date is within range

Points to Remember

Having discussed different ways to use dates with the IF function, here are some important points to remember:

  1. Instead of hardcoding the dates into the IF function’s logical test parameter, you can store the date in a separate cell and refer to it with a cell reference. For example, instead of typing =IF(A2<”05/07/2021”,”done”,”not done”), you can store the date 05/07/2021 in a cell, say B2 and type the formula: =IF(A2<B2,”done”,”not done”).

IF formula to compare two dates

  1. Alternatively, you can use the DATEVALUE function as explained in the first part of this tutorial.

Using IF function with Datevalue

  1. Another neat technique that you can use is to simply add a zero to the date (which has been enclosed in double quotes). So you can type: =IF(A2<”05/07/2021”+0,”done”,”not done”). This will make Excel take the date inside double quotes as a serial number, and use it in the logical test without having its value changed.

Adding 0 to date within IF formula

In this tutorial, I showed you some great techniques to use the IF function with dates. I hope the tips covered here were useful for you.

Other articles you may also like:

  • Multiple If Statements in Excel (Nested Ifs, AND/OR) with Examples
  • How to use Excel If Statement with Multiple Conditions Range [AND/OR]
  • How to Convert Month Number to Month Name in Excel
  • Why are Dates Shown as Hashtags in Excel? Easy Fix!
  • How to Convert Serial Numbers to Date in Excel
  • How to Get the First Day Of The Month In Excel?

Joe4

Joe4

MrExcel MVP, Junior Admin


  • #2

By writing it like:
A1<=»09:00″
you are checking to see if A1 is less than the text entry of 09:00 (double-quotes denotes text).

Assuming that you value in A1 has no date component, use this instead:
A1<=(9/24)

This is because Excel tracks time as numeric value that is the fraction of one day.

  • #3

That works but brings more questions to why my method didnt work:
I have another formula that works fine with the quotes.

Column A has ten percentage entries and B1 has a formula to count a particular percentage in the range.
Percent is numerical but the Greater-Than-Equal-To and Less-Than-Equal-To entries I have are in quotes. So why isn’t that treated as text?

Formula: =COUNTIFS(A1:A10,»>=90%»,A1:A10,»<=99.99%»)

90% 5
80%
50%
60%
23%
25%
99%
98%
96%
95%

Joe4

Joe4

MrExcel MVP, Junior Admin


  • #4

Notice that in your second formula, it is the entire condition (including the equality/inequality signs) that is in quotes, not just the number:
«>=90%»

In the first one, it is only the number in quotes, so the number is treated like text.
<=»09:00″

The location of the first quote makes all the difference in how it is treated.

Note that your original formula would work if you actually convert the text to time, like this:
=IF(A1<=TIMEVALUE(«09:00″),»TRUE»,»FALSE»)

  • #5

I appreciate you showing me all of this and answering my questions and even showing me how I can adjust a text format into a time value but it still brings questions.
If the quote placements or no quote placements to represent an object as numeric or text is important, the following examples below, for me, conflict:

Formula: =IF(A1″<=09:00″,»TRUE»,»FALSE»): Quotes encompass the mathematical equation and doesn’t work

Formula: =COUNTIFS(A1:A10,»>=90%»,A1:A10,»<=99.99%»): Quotes also encompass the mathematical equation and does work
I see no difference in order of operation or quotes placement but one works and one doesn’t. The only difference is one’s time and the other is a percent but both numeric, not text.

Additionally if no quotes represents numeric
Formula: =IF(A1<=09:00,»TRUE»,»FALSE»): No quotes at all doesn’t work.
I don’t get this one, if numerics are supposed to be represented with no quotes, the setup doesn’t turn the 09:00 into a text but this is no good either.

Again I’m always learning excel. Every time I get an answer I usually have more questions.
Thank you so much

Joe4

Joe4

MrExcel MVP, Junior Admin


  • #6

It is important to understand the requirements of each of the arguments of the different functions you are using.

In the IF function, the first argument requires a complete mathematical expression that results in a True/False value to be returned.
That can be accomplished by writing an equality/inequality formula, or by using a function that returns a boolean function (such as IsOdd, IsError, etc).
So:
A1<=»09:00″
A1<=(9/24)
A1<=TIMEVALUE(«09:00»)

are all valid boolean mathematical formulas (though they do not all do the same thing, as you discovered).

A1″<=09:00″
is NOT a valid formula. Remember, anything enclosed in quotes is treated as literal text. So in this instance, the «<=» is treated as text, not as an operator.

However, the COUNTIF function operates differently. The second argument does NOT want a complete formula that returns a boolean value.
All it wants is the condition of the formula (to be applied to the range in the first argument). It is not looking for the whole formula, only the condition, and it wants that condition returns as text.
So:
«>=90%»
«<=99.99%»

are valid conditions, but not complete formulas.
Since you can build the conditions dynamically, the literal parts are in double-quotes.

For example, instead of seeing if the values in your designated range are >=90%, suppose that you wanted to see if they were greater than or equal than the number in cell D1. Then you could write the condition like this:
«>=» & D1

Keep in mind that Excel has pretty good documentation on all their functions (the formula helper is useful), and there is lots of great information out there on the Web too, where you can find details explanations and examples of all these functions.

One final note on your original function:
=IF(A1<=(9/24),»TRUE»,»FALSE»)
Since you have «TRUE» and «FALSE» in double-quotes, this will return the text string TRUE and FALSE. If you wanted to return the boolean values, you would drop the quotes around the TRUE/FALSE. And actually, if you wanted to do that, you don’t even need the IF statement. You could write it as a straight-up boolen function like this:
=A1<=(9/24)
If that statement is True, it will return TRUE. If it is False, it will return FALSE.

Hope that helps.

  • #7

I appreciate that break down extremely. I can’t thank you enough.

Joe4

Joe4

MrExcel MVP, Junior Admin


  • #8

You are welcome.

The text/non-text thing often tricks people up at first (especially in writing VBA!).

The key fact to understand about date and times is that Excel actually stores them as numbers. So they are really just numbers with special date formats.
The number represents the number of days since 1/0/1900, and time is just the fraction of one day.
To see this in action, enter any date or time in Excel, and then change the format of that cell to «General». Then you will see it as Excel does.

I am trying to write an IF statement to look at a range for time and return either Shift A or Shift B.

This is my formula that isn’t working:

=IF(F2>TIME(6,0,0),IF(F2<TIME(16,0,0)),"A","B")

pnuts's user avatar

pnuts

58k11 gold badges85 silver badges137 bronze badges

asked Aug 23, 2013 at 14:55

ajaustin12's user avatar

1

Use =AND where both (or more) conditions have to be met to result in TRUE. Then the second IF is not required. With:

=IF(AND(F2>TIME(6,0,0),F2<TIME(16,0,0)),"A","B")  

Shift A applies to just after 06:00 hrs through to just before 16:00 hrs and Shift B otherwise. IF and F so intermingled in the same formula was a little unfortunate!

answered Aug 23, 2013 at 15:11

pnuts's user avatar

pnutspnuts

58k11 gold badges85 silver badges137 bronze badges

1

Data analysis in Excel often involves working with dates.

A common thing many Excel users need to check when working with dates is whether a date is between two given dates.

A simple use case of this could be when you need to check whether the date of submission of a report was within the given dates or not. Based on this, you can highlight what reports were submitted after the deadline.

In this tutorial, I will show you how to check if the date is between two given dates or not.

Using Nested IF Formula

One of the easiest ways to check whether a date is in between two given dates is by using a simple if formula.

And since we need to check for two conditions, we would need to use two if formulas.

And when you use an IF formula within another IF formula, that is called the nested IF construct.

Below I have a data set where I have the project start date and project end date in column A and column B respectively. And then I have the project submission date in column C.

Dataset to check if date lies between two dates

Now I want to check whether the project submission date was between the project start and project end date or not.

This can easily be done using the below nested IF formula:

=IF(C2>=A2,IF(C2<=B2,"In Range","Out of Range"),"Out of Range")
Formula to check date in between two dates

The above formula would return ‘In Range’ if the date lies in between the two given dates, and it would return ‘Out of Range’ in case the date is either before the project started or after the project end date.

Now let me quickly explain how this formula works.

I first used and if formula to check whether the date is after the project start date or not.

Based on these criteria, I need to specify what should the formula do in case this condition is true, and what should the formula do in case the condition is not true.

But since I have two conditions to check, immediately after I checked the first condition, I use the second IF function to check for the other condition (which is whether the date is before the project end date or not).

Since the IF function takes 3 arguments (the condition, value when the condition is True, and value in the condition is False), for the second IF function, I specify ‘In Range’ as the second argument, as it has satisfied both the conditions, and I specify out of range as the third argument, because it satisfies the first if condition, but it fails the second if condition.

And then finally, I specify ‘Out of Range’ as the second argument for the first IF function (which means that the condition in the first IF function failed, and hence it did not go to the second if function and instead returned the second argument of the first IF function).

Since I had only two conditions to check, I have used two if functions where the second function is nested within the first one. In case you have more than two conditions to check you can further nest these if functions (although it tends to get a bit complicated after a few)

Also read: Avoid Nested IF Function in Excel…VLOOKUP to Rescue

Using IF + AND Formula

While many Excel users are quite comfortable using the IF formula, when you have multiple conditions to check, I prefer using the combination of IF and AND formula instead.

Within the AND formula, you can check for multiple conditions, and can specify what result you should get in case all the conditions are true, and the result you should get in case any of these conditions are FALSE.

Let’s again take the same data set where I have the project started and project end date in column A and column B, and I have the project submission date in column C.

Dataset to check if date lies between two dates

Below is the formula I can use to check whether the submission date lies between the project start date and project end date or not:

=IF(AND(C2>=A2,C2<=B2),"In Range","Out of Range")
IF and AND formula to check in between two dates

The above formula checks for both the conditions and it would return ‘In Range’ in case the submission date is in between the start and the end date, and it would return ‘Out of Range’ in case the submission date is before the project started or after the project end date.

Note that I have still used an IF function in the above formula, however, I didn’t have the need to use two if functions.

Since I had to check for both the conditions, I did that using the AND function instead.

The AND function would return TRUE if both the conditions are true, and it would return FALSE if any or both the conditions are false.

And since I needed a more descriptive output instead of a simple TRUE or FALSE, I have used an IF function where it would give me ‘In Range’ in case the result is TRUE and ‘Out of Range’ in case the result is FALSE.

Check If the Date Occurs on Weekend

A common use case when working with dates in project management is to identify whether a date occurs on a weekend or not.

If you’re checking this manually, you would check whether a date is a Saturday or Sunday.

But when working with dates in Excel, you can use the WEEKDAY function that can easily do this for you.

Below I have a data set where I have some dates in column A, and I want to check whether these dates occur on a weekend or not.

Check if Date is weekend

For the purpose of this tutorial, I would consider Saturday and Sunday as the weekend days.

Below is the formula that will do this for you:

=WEEKDAY(A2,2)>5
WEEKDAY formula to check the date

If the date occurs on a Saturday or Sunday, it will give you a TRUE, else it will give a FALSE.

The above WEEKDAY formula checks the serial number of the date, and returns a number that corresponds to the weekday number for that date. So if it’s a Monday, it would return 1, if it’s a Tuesday it would return 2, and so on.

Since the condition that I’m checking is whether the weekday result is more than 5, it would return TRUE if the date is on a weekend, and it would return FALSE if it’s on a weekday.

If you want the result to be more meaningful, you can use the below if formula, which will return ‘Weekday’ in case the date occurs on a weekday and ‘Weekend’ in case the date occurs on a weekend.

=IF(WEEKDAY(A2,2)>5,"Weekend","Weekday")

Issues When Checking Whether a Date is in Between two dates

So far, I’ve shown you a couple of scenarios where you can check whether a date lies between two given dates.

In all the formulas, the underlying principle is to compare the value of the given date with the start and the end date. If the value of the given date, is in between the start and the end date, then it occurs in between these two dates, else it does not.

However, in some cases, you may see some unexpected results.

In this section, I cover some common pitfalls that you need to be aware of when comparing dates in Excel:

Dates Need to Be in the Right Format

Dates and time values are stored as numbers in the back-end in Excel. So when you compare two dates, you’re essentially comparing two numbers.

You may think that the date looks nothing like a number (for example 01 January 2023), but remember that dates are formatted to show up differently in the cell in Excel, while in the back-end these are still numbers.

For example, a cell may show 01 January 2023, but in the backend, the value of this cell would be 44927 (which indicates the total number of days that have elapsed after 01 Jan 1900).

Now, if your dates are in the right format, all is well.

But there is also a possibility that your date is in a format that Excel does not recognize as a date, which means that instead of a number in the back end, it ends up being considered a text string.

For example, Jan 01, 2023, is not a valid date format in Excel.

So if you have this in a cell in excel, it would be considered a text string, and using this to compare it with other dates can give you incorrect results.

Bottom line – When comparing dates in Excel, make sure that the dates are in the right format

Also read: How to Change Date Format In Excel?

Dates May have a Time Part that’s Hidden

Just like dates, time values are also stored as numbers in the backend in Excel.

While a whole number would indicate a full day, the decimal part would indicate the portion of the day or the time value for that day.

For example, if you have 01-01-2023 18:00:00 in a cell in Excel, in the backend it would be 44927.75, where 44927 means 01-01-2023 and 0.75 means 18:00:00

Now, here’s the problem that you can encounter when checking whether a date lies in between two given dates.

You may clearly see that the date is in between the two given dates, but Excel may give you a different result.

This can happen because most of the time when people work with dates and times, the time portion is hidden so that you only see the date but you do not see the time value.

Below I have a simple example where I have the same date in cells A1 and B1, but when I compare these two cells, it tells me these are not the same.

Date look same but are different

While you can clearly see that these dates are exactly the same, what you do not see is that there is a time value in cell A1 that is hidden so you do not see it. But when excel compares these two cells, it considers these as different (which it rightly should).

While this is not such a common scenario, if this happens, it can sometimes stump even advanced Excel users.

In this tutorial, I showed you a couple of simple formulas that you can use to check whether a date is between two given dates or not.

I’ve also mentioned some of the pitfalls you should be aware of when comparing dates and Excel.

I hope you found this Excel tutorial useful.

Other Excel Tutorials you may also like:

  • Calculate the Number of Months Between Two Dates in Excel
  • How to Calculate the Number of Days Between Two Dates in Excel
  • How to Add or Subtract Days to a Date in Excel
  • How to Stop Excel from Changing Numbers to Dates Automatically
  • How to Add Months to Date in Excel
  • How to Get the Number of Days in a Month in Excel?
  • Get Day Name from Date in Excel

Explanation 

In this example, the goal is to check if a given date is between two other dates, labeled «Start» and «End» in the example shown. For convenience, both start (E5) and end (E8) are named ranges. If you prefer not to use named ranges, make sure you use absolute references for E5 and E8.

Excel dates

Excel dates are just large serial numbers and can be used in any numeric calculation or comparison. This means we can simply compare a date to another date with a logical operator like greater than or equal (>=) or less than or equal (<=).

AND function

The main task in this example is to construct the right logical test. The first comparison is against the start date. We want to check if the date in B5 is greater than or equal (>=) to the date in cell E5, which is the named range start:

=B5>=start

The second expression needs to check if the date in B5 is less than or equal (<=) to the end date in cell E5:

=B5<=end

The goal is to check both of these conditions are TRUE at once, and for that, we use the AND function:

=AND(B5>=start,B5<=end) // returns TRUE or FALSE

The AND function will return TRUE when the date in B5 is greater than or equal to start AND less than equal to end. If either test fails, the AND function will return FALSE. We now have the logic we need to use with the IF function.

IF function

We start off by placing the expression above inside the IF function as the logical_test argument:

=IF(AND(B5>=start,B5<=end)

Next, we add a value_if_true argument. In this case, we  want to return an «x» when a date is between two dates, so we add «x» as a text value:

=IF(AND(B5>=start,B5<=end),"x"

If the date in B5 is not between start and end, we don’t want to display anything, so we use an empty string («») for value_if_false. The final formula in C5 is:

=IF(AND(B5>=start,B5<=end),"x","")

As the formula is copied down, the formula returns «x» if the date in column B is between the start and end date. If not, the formula returns an empty string («»), which looks like an empty cell in Excel. The values returned by the IF function can be customized as desired.

Понравилась статья? Поделить с друзьями:
  • If and iserror excel
  • If formula excel 2016
  • If and index function in excel
  • If formula advanced excel
  • If and iferror in excel