You are on page 1of 8

CHAPTER 6

Dates and date functions

Questions answered in this chapter:


■■ When I enter dates in Excel, I often see a number such as 37625 rather than a date such as
1/4/2003. What does this number mean, and how can I change it to a normal date?

■■ Can I use a formula to display today’s date automatically?

■■ How can I determine a date that is 50 workdays after another date? How can I exclude holi-
days if I want to?

■■ How can I determine the number of workdays between two dates?

■■ I have 500 dates entered in an Excel worksheet. How can I write formulas to extract from each
date the month, year, day of the month, and day of the week?

■■ I am given the year, month, and day of the month for a date. Is there an easy way to recover
the actual date?

■■ My business has purchased and sold machines. For some, I have the date the machine was
purchased and the date the machine was sold. Can I easily determine how many months we
kept these machines?

To illustrate the most commonly used month-day-year formats in Microsoft Excel 2013, suppose
today is January 4, 2004. You can enter this date as any of the following:

■■ 1/4/2004

■■ 4-Jan-2004

■■ January 4, 2004

■■ 1/4/04

If you enter only two digits to represent a year, and the digits are 30 or higher, Excel assumes the
digits represent years in the twentieth century; if the digits are lower than 30, Excel assumes they rep-
resent years in the twenty-first century. For example, 1/1/29 is treated as January 1, 2029, but 1/1/30
is treated as January 1, 1930. Each year, the year treated as dates in the twenty-first century increases
by one.

51

Answers to this chapter’s questions
This section provides the answers to the questions that are listed at the beginning of the chapter.

When I enter dates in Excel, I often see a number such as 37625 rather than a date such as
1/4/2003. What does this number mean, and how can I change it to a normal date?

The way Excel treats calendar dates is sometimes confusing to the novice. The key is understanding
that Excel can display a date in a variety of month-day-year formats, or it can display a date in serial
format. A date in serial format, such as 37625, is simply a positive integer that represents the number
of days between the given date and January 1, 1900. Both the current date and January 1, 1900 are
included in the count. For example, Excel displays January 3, 1900, in serial format as the number
3, which means there are three days between January 1, 1900, and January 3, 1900 (including both
days).

Note  Excel assumes that 1900 was a leap year containing 366 days. In reality, 1900 con-
tained only 365 days. For some fascinating information on the origin of this bug, see www.
joelonsoftware.com/items/2006/06/16.html.

Figure 6-1 shows the worksheet named Serial Format in the Dates.xlsx file. Suppose you are given
the dates shown in cells D5:D14 in serial format. For example, value 37622 in cell D5 indicates a date
that is 37,622 days after January 1, 1900 (including both January 1, 1900, and day 37,622). To display
these serial dates in month-day-year format, copy them to E5:E14. Select the E5:E14 cell range, right-
click the selection, and choose Format Cells. At any time, by the way, you can open the Format Cells
dialog box by pressing Ctrl+1. Now select the date format you want from the list shown in Figure 6-2.
The dates in E5:E14 are displayed in date format, as you can see in Figure 6-1. If you want to format
dates in the serial number format, select E5:E14, right-click the selection, and choose Format Cells,
General.

FIGURE 6-1  Use the Format Cells command to change dates from serial number format to month-day-year
format.

52  Chapter 6  Dates and date functions


Simply changing the date format of a cell to General will yield the date in serial format. Another
way to obtain the date in serial format is to use the DATEVALUE function and enclose the date in
quotation marks. For example, in the Date Format worksheet of the Dates.xlsx file, cell I5 contains the
DATEVALUE(“1/4/2003”) formula. Excel yields 37625, which is the serial format for January 4, 2003.

FIGURE 6-2  Reformat a serial number to month-day-year format as this figure shows.

Can I use a formula to display today’s date automatically?

Displaying today’s date with a formula is easy, as you can see by looking at cell C13 of the Date
Format worksheet shown in Figure 6-3. Entering the TODAY() function in a cell displays today’s date.
This screenshot was created on July 7, 2013.

Dates and date functions  Chapter 6   53


FIGURE 6-3  Here are examples of date functions.

How can I determine a date that is 50 workdays after another date? How can I exclude holi-
days if I want to?

The WORKDAY(start_date,#days,[holidays]) function displays the date that is the number of work-
days (a workday is a nonweekend day) indicated by #days after a given start date. Holidays is an
optional argument for the function by which you can exclude from the calculation any dates that are
listed in a cell range. Thus, entering the WORKDAY(C14,50) formula in cell D14 of the Date Format
worksheet tells you that 3/14/2003 is 50 workdays after 1/3/2003. If you believe that the only two
holidays that matter are Martin Luther King Day and Independence Day, you can change the formula
to WORKDAY(C14,50,F17:F18). With this addition, Excel does not count 1/20/2003 in its calculations,
making 3/17/2003 the fiftieth workday after 1/3/2003. Note that instead of referring to the holidays
in other cells, you can enter them directly in the WORKDAY formula with the serial number of each
holiday enclosed in curly brackets ({ }). For example, WORKDAY(38500,10,{38600,38680,38711}) would
find the tenth workday after the date with serial number 38500, ignoring Labor Day, Thanksgiving,
and Christmas 2005.

The WORKDAY.INTL function was introduced in Excel 2010. Use this function to choose your own
definition of a workday. The syntax is WORKDAY.INTL(start_date,days,weekend,[holidays]). Use the
third argument to specify the definition of a day off. You can use the following codes to specify the
definition of a day off:

1 or omitted Saturday, Sunday


2 Sunday, Monday
3 Monday, Tuesday
4 Tuesday, Wednesday
5 Wednesday, Thursday
6 Thursday, Friday

54  Chapter 6  Dates and date functions


7 Friday, Saturday
11 Sunday only
12 Monday only
13 Tuesday only
14 Wednesday only
15 Thursday only
16 Friday only
17 Saturday only

For example, in worksheet Date Format of the Dates.xlsx file, the date 100 workdays after
3/14/2011 was computed in cell C27 (see Figure 6-4), with Sunday and Monday as days off, with the
WORKDAY.INTL(C23,100,2) formula. In cell C28, the date 100 workdays after 3/14/2011 when only
Sunday is a day off was computed by using the WORKDAY.INTL(C23,100,11) formula. You can also
enter the definition of days off with a string of seven ones and zeroes; a one indicates a day off, and
the first entry in the string is Monday, the second is Tuesday, and so on. Thus, in cells D26 and D27,
the previous results were duplicated with the WORKDAY.INTL(C23,100,”1000001”) and WORKDAY.
INTL(C23,100,”0000001”) formulas, respectively.

How can I determine the number of workdays between two dates?

The key to solving this problem is to use the NETWORKDAYS function. The syntax for this function is
NETWORKDAYS(start_date,end_date,[holidays]), where holidays is an optional argument identifying a
cell range that lists the dates you want to count as holidays. The NETWORKDAYS function returns the
number of working days between start_date and end_date, excluding weekends and any listed holi-
days. As an illustration of the NETWORKDAYS function, look at cell C18 in the Date Format worksheet,
which contains the NETWORKDAYS(C14,C15) formula. This formula yields the number of working days
between 1/3/2003 and 8/4/2003, which is 152. The NETWORKDAYS(C14,C15,F17:F18) formula in cell
C17 yields the number of workdays between 1/3/2003 and 8/4/2003, excluding Martin Luther King
Day and Independence Day. The answer is 152–2=150.

The NETWORKDAYS.INTL function was introduced with Excel 2010. Like the WORKDAYS.INTL
function, NETWORKDAYS.INTL enables you to customize the definition of a weekend. For exam-
ple, in cell C31 of the Date Format worksheet in the Dates.xlsx file, the number of workdays (373)
between 3/14/2011 and 8/16/2012 was computed, when Sunday and Monday are days off, with the
NETWORKDAYS.INTL(C23,C24,2) formula. (See Figure 6-4.) In cell D31, the same result was computed
with the NETWORKDAYS.INTL(C23,C24,”1000001”) formula.

Dates and date functions  Chapter 6   55


FIGURE 6-4  This figure shows examples of international date functions.

I have 500 dates entered in an Excel worksheet. How can I write formulas that will extract
from each date the month, year, day of the month, and day of the week?

The Date Format worksheet of the Dates.xlsx file (see Figure 6-3) lists several dates in the B5:B10 cell
range. In B5 and B7:B9, four formats were used to display January 4, 2003. In columns D:G, the year,
month, day of the month, and day of the week for each date were extracted. By copying the YEAR(B5)
formula from D5 to D6:D10, the year for each date was extracted. By copying the MONTH(B5) formula
from E5 to E6:E10, the month (1=January, 2=February, and so on) portion of each date was extracted.
By copying the DAY(B5) formula from F5 to F6:F10, the day of the month for each date was extracted.
Finally, by copying the WEEKDAY(B5,1) formula from G5 to G6:G10, the day of the week for each date
was extracted. Note that this process might not work for non-US regional settings.

When the last argument of the WEEKDAY function is 1, 1=Sunday, 2=Monday, and so on. When
the last argument is 2, 1=Monday, 2=Tuesday, and so on. When the last argument is 3, 0=Monday,
1=Tuesday, and so on.

I am given the year, month, and day of the month for a date. Is there an easy way to recover
the actual date?

The DATE function, whose arguments are DATE(year,month,day), returns the date with the given year,
month, and day of the month. In the Date Format worksheet, copying the DATE(D5,E5,F5) formula
from cell H5 to cells H6:H10 recovers the dates you started with.

My business has purchased and sold machines. For some, I have the date the machine was
purchased and the date the machine was sold. Can I easily determine how many months we
kept these machines?

The DATEDIF function can easily determine the number of complete years, months, or days between
two dates. In file Datedif.xlsx (see Figure 6-5), you can see that a machine was bought on 10/15/2006
and was sold on 4/10/2008. How many complete years, months, or days was the machine kept?
The syntax of the DATEDIF function is DATEDIF(startdate,enddate,time_unit). If the unit is written

56  Chapter 6  Dates and date functions


as “y,” you get the number of complete years between the start and end dates; if the unit is written
as “m,” you get the number of complete months between the start and end dates; and if the unit is
written as “d,” you get the number of complete days between the start and end dates. Thus, enter-
ing DATEDIF(D4,D5,”y”) in cell D6 shows that the machine was kept for one full year. Entering the
DATEDIF(D4,D5,”m”) formula in cell D7 shows the machine was kept for 17 complete months. Entering
the DATEDIF(D4,D5,”d”) formula in cell D7 shows the machine was kept for 543 complete days.

FIGURE 6-5  This figure shows use of the DATEDIF function.

Problems
1. What is the serial format for January 25, 2006?

2. What is the serial format for February 14, 1950?

3. To what actual date does a serial format of 4526 correspond?

4. To what actual date does a serial format of 45000 correspond?

5. Determine the day that occurs 74 workdays after today’s date (including holidays).

6. Determine the day that occurs 74 workdays after today’s date (including holidays but exclud-
ing the current year’s Christmas, New Year’s Day, and Independence Day).

7. How many workdays (including holidays) are there between July 10, 2005, and August 15,
2006?

8. How many workdays (including holidays but excluding Christmas, New Year’s Day, and
Independence Day) are there between July 10, 2005, and August 15, 2006?

Note  The Datep.xlsx file contains several hundred dates. Use this file for the next set
of problems.

9. Determine the month, year, day of the month, and day of the week for each date.

10. Express each date in serial format.

Dates and date functions  Chapter 6   57


11. A project begins on December 4, 2005. The project consists of Activities 1, 2, and 3. Activity 2
can start the day after Activity 1 finishes. Activity 3 can start the day after Activity 2 finishes.
Set up a worksheet that accepts as inputs the duration (in days) of the three activities and
outputs both the month and year during which each activity is completed.

12. You bought a stock on July 29, 2005, and sold it on December 30, 2005. The stock exchange
is closed on Labor Day, Christmas, and Thanksgiving. Create a list of dates of when the stock
market was open during the time you owned the stock.

13. The Machinedates.xlsx file contains dates on which several machines were bought and sold.
Determine how many months and years each machine was kept.

14. Given any date, find a way to have Excel compute the day of the week of the first day of the
date’s month.

15. Given any date, find a way to have Excel compute the last day of the date’s month. Hint:
DATE(2005,13,1), surprisingly, returns 1/1/2006.

16. Given any date, how can you compute which day of the year it is? For example, what day of
the year will 4/15/2020 be? Hint: The DATE(2020,1,0) formula will return the serial number for
day 0 of January 2020, and Excel will treat this as December 31, 2019.

17. In the country of Fredonia, employees have Tuesday and Wednesday off. What date is 200
workdays after 5/3/2013?

18. In the country of Lower Ampere, employees have Friday and Saturday off. In addi-
tion, Valentine’s Day is a holiday. How many workdays are there between 1/10/2014 and
5/31/2015?

19. Suppose the first quarter of each year is January–March, the second quarter is April–June, and
so on. Write a formula that returns the first day of the quarter for a given date.

20. Use the definitions of quarters given in problem 19 and write a formula that computes the last
day of the previous quarter for any given date.

21. Set up a spreadsheet in which a person can enter his date of birth, and the spreadsheet
returns the person’s actual age.

22. Memorial Day is always the last Monday in May. Set up a spreadsheet in which you can enter
the year, and the spreadsheet determines the date of Memorial Day.

23. Create a spreadsheet that always lists the next 50 workdays (assuming no holidays and that
Monday through Friday are the workdays.)

58  Chapter 6  Dates and date functions

You might also like