Sub topic 4
Adding and subtracting dates
This chapter assumes you can enter formulas, use cell references such as A2, and apply a number format to a cell. It also assumes you know that Excel stores a date as a serial number, a count of days, as covered in the earlier chapters. Here you use that fact to do arithmetic with dates.
The two rules
Because a date is a number, ordinary arithmetic works on it. Two rules cover nearly every case:
- A date plus or minus a number is a date. The number is a count of days.
- A date minus a date is a number. The number is the days between them.
Adding two dates is allowed but almost never meaningful. The result is a serial number far in the future.
Adding days to move a date
Adding a whole number moves a date forward. Subtracting moves it back. Excel handles month lengths and leap years for you, because it only increments a counter.
=DATE(2024,1,31)+30The serial number for 31 January 2024 is 45322. Adding 30 gives 45352, which is 1 March 2024. February 2024 has 29 days, so 29 days after 31 January is 29 February, and the thirtieth day is 1 March.
A common use is a due date. If an invoice date is in A2 and payment is due 30 days later:
=A2+30Put the number of days in its own cell, for example B1, instead of typing 30 into every formula. Then the formula is =A2+$B$1, and you change the payment terms in one place.
Adding weeks is multiplication. Seven days is one week, so =A2+2*7 moves two weeks ahead.
Subtracting dates to count days
Subtracting the earlier date from the later one gives the number of days between them.
=DATE(2024,3,1)-DATE(2024,1,1)The serial numbers are 45352 and 45292, so the result is 60. The difference depends on the actual calendar, so a span that includes 29 February 2024 is one day longer than the same span in 2023.
=DATE(2023,3,1)-DATE(2023,2,1)This returns 28, because February 2023 has 28 days.
To get weeks, divide by 7. =(B2-A2)/7 returns a fraction when the span is not a multiple of 7. Wrap it in INT for complete weeks: =INT((B2-A2)/7).
Inclusive and exclusive counting
Subtraction counts the number of midnights crossed, not the number of calendar days touched. From 1 January to 3 January, =DATE(2024,1,3)-DATE(2024,1,1) returns 2. If a project runs on the first, second, and third, it occupies three calendar days. In that case add 1:
=DATE(2024,1,3)-DATE(2024,1,1)+1This returns 3. Decide which counting rule your situation needs before you build the formula. A hotel stay from the first to the third is 2 nights. A conference running from the first to the third is 3 days.
The result cell needs the right format
Excel tries to guess a format when you type a formula. When you subtract two dates, it often formats the result as a date as well. Then a result of 60 appears as 29/02/1900 or something similar, which looks like an error.
The value is correct. Only the format is wrong. Select the cell and apply the General or Number format, and the 60 appears.
The opposite problem happens when you add days to a date and the cell is formatted as General. You see 45352 instead of 1 March 2024. Apply a date format to fix it.
| Formula | Result is | Format to apply |
|---|---|---|
| `=A2+30` | A date | A date format |
| `=A2-7` | A date | A date format |
| `=B2-A2` | A count of days | General or Number |
| `=(B2-A2)/7` | A count of weeks, possibly fractional | Number |
Negative results
If you subtract in the wrong order, the answer is negative. With General or Number format, you see a negative number such as -60. That is useful information: it tells you the second date is earlier than the first.
If the cell has a date format, a negative serial number cannot be shown as a date in the 1900 system, and the cell fills with #####. The cause is the format, not the formula. Change the cell to General to see the negative number.
You can control the sign explicitly:
=MAX(0,TODAY()-B2)returns days overdue, and 0 when the due date has not arrived.=ABS(B2-A2)returns the gap between two dates whichever is earlier.
The TODAY function is covered in the next chapter. For now, treat it as a function that returns today's date.
Dates with a time part
If a cell holds a date and a time, its serial number has a fraction. The time of day is covered in the next section of the tutorial. For now, remember the effect on subtraction: the difference includes the fractional day.
=B2-A2If A2 is 1 March 2024 at 18:00 and B2 is 2 March 2024 at 06:00, the result is 0.5, half a day. If you want the number of calendar dates crossed, remove the time parts first:
=INT(B2)-INT(A2)This returns 1. INT drops the fraction, which moves each value to midnight at the start of its day. Use it any time a date column might have times attached, such as data exported from a system that records timestamps. A comparison like =A2=B2 can also fail for two values that look like the same date, because one carries a time.
Comparing dates
Comparison operators work on the serial numbers, so earlier dates are smaller:
=IF(B2<TODAY(),"Overdue","Open")This returns "Overdue" when the due date in B2 is before today. It returns "Open" for a due date of today or later. The same logic works in conditional formatting, in COUNTIF criteria such as "<"&DATE(2024,1,1), and in filters.
Adding months and years: where plain addition fails
Adding days is exact. Adding months or years with plain + is not, because months have different lengths.
=A2+30does not move to the same day next month. It moves 30 days, which is a different calendar day in most months.=A2+365does not move exactly one year in a leap year.
For whole months, use EDATE, which the later chapters cover. For years, you can rebuild the date with DATE:
=DATE(YEAR(A2)+1,MONTH(A2),DAY(A2))This returns the same day and month one year later. It has an edge case. If A2 is 29 February 2024, the formula asks for 29 February 2025. That date does not exist, and DATE rolls over to 1 March 2025. This overflow behavior is also why DATE(2024,1,32) returns 1 February 2024. It can be an advantage when you want it and a surprise when you don't.
Text that looks like a date
Arithmetic needs numbers. If a "date" is text, for example imported from a CSV file, the formula may fail or may quietly give a wrong answer.
- A text value that Excel can parse may be converted during arithmetic. This depends on your regional settings, so a formula that works on your machine can break on a colleague's.
- A text value that Excel cannot parse returns
#VALUE!. - Text left-aligns in a cell by default, while real dates right-align. This is a quick visual check.
To test, use =ISNUMBER(A2). It returns TRUE for a real date and FALSE for text. To convert reliably, build the date from its parts with DATE, or use DATEVALUE on text in a format your settings recognize. Later chapters return to this in more detail.
A worked example: a small task tracker
Suppose columns hold these values in row 2: A2 start date, B2 duration in days, C2 end date, and D2 days remaining.
=A2+B2Put this in C2 and format it as a date. It gives the end date.
=MAX(0,C2-TODAY())Put this in D2 and format it as General. It gives the days left, and shows 0 once the end date has passed. Because TODAY() changes daily, D2 will change each time the workbook recalculates on a new day, while C2 stays fixed.
The two formulas show the whole chapter in small form. One adds a day count to a date and returns a date. The other subtracts a date from a date and returns a count.
Common mistakes
- Reading a date-formatted subtraction result as a date, such as
29/02/1900for 60 days. - Reading
#####as a formula error when the cell is too narrow or the result is a negative date. - Using
+30for "next month". - Forgetting that stored times make two same-day values unequal.
- Treating imported text as dates.
Exercise
Cell A2 holds the date 10 March 2024 and cell B2 holds 25 April 2024, both as real dates. You write =B2-A2 in C2 and see 01/02/1900 on screen. (1) What is the real result and why does it display that way? (2) What formula gives the number of complete weeks between the dates?
Show answer
(1) March has 31 days, so 10 March to 31 March is 21 days. Adding the 25 days of April gives 46 days. Serial 46 in the 1900 system is shown as 15 February 1900, so the exact on-screen text depends on your locale, but the point is that the cell is formatted as a date. Excel inherited the date format from the inputs. Change C2 to General or Number to see 46. (2) =INT((B2-A2)/7) returns 6, since 46/7 is about 6.57 and INT drops the fraction.
Exercise
A1 holds the timestamp 1 June 2024 22:00 and B1 holds the timestamp 2 June 2024 02:00. What does =B1-A1 return, and what does =INT(B1)-INT(A1) return? Which one tells you how many calendar dates were crossed?
Show answer
=B1-A1 returns about 0.1667, four hours expressed as a fraction of a day (4/24). =INT(B1)-INT(A1) returns 1, because INT strips the time and the dates differ by one day. The second formula tells you how many calendar dates were crossed.