Professional Documents
Culture Documents
Week 3.
Using Formulas with Dates and Times
Entering dates
You can enter a date directly as a serial number (if you know the serial number) and then format
it as a date. More often, you enter a date by using any of several recognized date for-mats. Excel
automatically converts your entry into the corresponding date serial number (which it uses for
calculations), and it applies the default date format to the cell so that it displays as an actual date
rather than as a cryptic serial number.
For example, if you need to enter June 18, 2022, into a cell, you can enter the date by typing June
18, 2022 (or any of several different date formats). Excel interprets your entry and stores the
value 44730, the date serial number for that date. It also applies the default date format, so the
cell contents may not appear exactly as you typed them.
Entering times
As with entering dates, you normally don’t have to worry about the actual time serial numbers.
Just enter the time into a cell using a recognized format. Table 1 shows some examples of time
formats that Excel recognizes.
TABLE 1. Time Entry Formats Recognized by Excel
1|P a g e
Data and Computing Skills
2|P a g e
Data and Computing Skills
If you type 2/29/1901, however, Excel correctly interprets it as a mistake and doesn’t convert it
to a date. Instead, it simply makes the cell entry a text string.
How can a product used daily by millions of people contain such an obvious bug? The answer is
historical. The original version of Lotus 1--2--3 contained a bug that caused it to treat 1900 as a
leap year. When Excel was released sometime later, the designers knew about this bug and chose
to reproduce it in Excel to maintain compatibility with Lotus 1--2--3 worksheet files.
Why does this bug still exist in later versions of Excel? Microsoft asserts that the disadvantages
of correcting this bug outweigh the advantages. If the bug were eliminated, it would mess up
millions of existing workbooks. In addition, correcting this problem would possibly affect
compatibility between Excel and other programs that use dates. As it stands, this bug really
causes very few problems because most users don’t use dates prior to March 1, 1900.
Pre-1900dates
The world, of course, didn’t begin on January 1, 1900. People who use Excel to work with
historical information often need to work with dates before January 1, 1900. Unfortunately, the
only way to work with pre--1900 dates is to enter the date into a cell as text. For example, you
can enter July 4, 1776, into a cell, and Excel won’t complain.
Using text as dates works in some situations, but the main problem is that you can’t perform
manipulation on a date that’s entered as text. For example, you can’t change its numeric
formatting, you can’t determine which day of the week this date occurred, and you can’t calculate
the date that occurs seven days later.
3|P a g e
Data and Computing Skills
These functions leverage the fact that, beneath the covers, dates and times are nothing more
than a numbering system. This opens the door for all kinds of cool formula--driven analyses.
The NOW() function returns the current date along with the current time:
=NOW()
Both the TODAY and NOW functions return date serial numbers that represent the current system
date and time. The TODAY function assumes 12:00 AM as the time, while the NOW function
returns the actual time.
It’s important to note that both of these functions will automatically recalculate each time you
change or open your workbook, so don’t use these functions as a timestamp of record.
Apart from this obvious use, the Excel TODAY function can be part of more complex formulas and
calculations based on today's date (See table 2). For example, to add 7 days to the current date,
enter the following formula in a cell:
=TODAY()+7
4|P a g e
Data and Computing Skills
To add 30 weekdays to today's date excluding weekend days, use this one:
=WORKDAY(TODAY(), 30)
TABLE 2. =TODAY
DATE function
DATE(year, month, day) returns a serial number of a date based on the year, month and day
values that you specify.
When it comes to working with dates in Excel, DATE is the most essential function to understand.
The point is that other Excel date functions not always can recognize dates entered in the text
format. So, when performing date calculations in Excel, you'd better supply dates using the DATE
function to ensure the correct results.
=DATE(YEAR(TODAY()), MONTH(TODAY()), 1) - returns the first day of the current year and
month.
=DATE(2015, 5, 20)-5 - subtracts 5 days from May 20, 2015.
5|P a g e
Data and Computing Skills
DAY function
DAY(serial_number) function returns a day of the month as an integer from 1 to 31.
Serial_number is the date corresponding to the day you are trying to get. It can be a cell reference,
a date entered by using the DATE function, or returned by other formulas.
Here are a few formula examples:
=DAY(A2) - returns the day of the month from a date in A2
=DAY(DATE(2015,1,1)) - returns the day of 1-Jan-2015
=DAY(TODAY()) - returns the day of today's date
MONTH function
MONTH(serial_number) function in Excel returns the month of a specified date as an integer
ranging from 1 (January) to 12 (December).
For example:
=MONTH(A2) - returns the month of a date in cell A2.
=MONTH(TODAY()) - returns the current month.
YEAR function
YEAR(serial_number) returns a year corresponding to a given date, as a number from 1900 to
9999.
The Excel YEAR function is very straightforward and you will hardly run into any difficulties
when using it in your date calculations:
=YEAR(A2) - returns the year of a date in cell A2.
6|P a g e
Data and Computing Skills
WEEKDAY function
WEEKDAY(serial_number,[return_type]) function returns the day of the week corresponding
to a date, as a number from 1 (Sunday) to 7 (Saturday).
=WEEKDAY(A2) - returns the day of the week corresponding to a date in cell A2; the 1st day of
the week is Sunday (default).
=WEEKDAY(A2, 2) - returns the day of the week corresponding to a date in cell A2; the week
begins on Monday.
=WEEKDAY(TODAY()) - returns a number corresponding to today's day of the week; the week
begins on Sunday.
7|P a g e
Data and Computing Skills
EDATE function
EDATE(start_date, months) function returns the serial number of the date that is the specified
number of months before or after the start date.
For example:
=EDATE(A2, 5) - adds 5 months to the date in cell A2.
=EDATE(TODAY(), -5) - subtracts 5 months from today's date.
EOMONTH function
EOMONTH(start_date, months) function returns the last day of the month a given number of
months from the start date.
Like most of Excel date functions, EOMONTH can operate on dates input as cell references,
entered by using the DATE function, or results of other formulas.
A positive value in the months argument adds the corresponding number of months to the
start date, for example:
=EOMONTH(A2, 3) - returns the last day of the month, 3 months after the date in cell A2.
A negative value in the months argument subtracts the corresponding number of months from
the start date:
=EOMONTH(A2, -3) - returns the last day of the month, 3 months before the date in cell A2.
8|P a g e
Data and Computing Skills
A zero in the months argument forces the EOMONTH function to return the last day of the start
date's month:
=EOMONTH(DATE(2015,4,15), 0) - returns the last day in April, 2015.
To get the last day of the current month, enter the TODAY function in the start_date argument
and 0 in months:
=EOMONTH(TODAY(), 0)
Using NETWORKDAYS.INTL
The one drawback to using the NETWORKDAYS function is that it defaults to excluding Saturday
and Sunday. But what if you work in a region where the weekends are actually Fridays and
Saturdays? Or worse yet, what if your weekends only include Sundays?
9|P a g e
Data and Computing Skills
Excel has you covered with the NETWORKDAYS.INTL function. In addition to the required start
and end dates, this function has an optional third argument—-a weekend code. The weekend
code allows you to specify which days to exclude as a weekend day.
As you enter the NETWORKDAYS.INTL function, Excel displays a menu as soon as you go into
the third argument (see Figure 2). Simply select the appropriate weekend code and press Enter.
Figure 2. NETWORKDAY.INTL allows you to specify which days to exclude as weekend days.
Calculating age
One of the easiest ways to calculate the age of anything is to use Excel’s DATEDIF function.
This function makes calculating any kind of date comparisons a breeze.
To calculate a person’s age using the DATEDIF function, you can enter a formula like this:
=DATEDIF("5/16/1972",TODAY(),"y")
The DATEDIF function calculates the number of days, months, or years between two dates. It
requires three arguments—-a start date, an end date, and a time unit.
The time units are defined by a series of codes listed in Table 3.
TABLE 3. DATEDIF Time Unit Codes
10 | P a g e
Data and Computing Skills
Using these time codes, you can easily calculate the number of years, months, and days between
two dates. If someone was born on May 16, 1972, you could find that person’s age in years,
months, and days using these respective formulas:
=DATEDIF("5/16/1972",TODAY(),"y")
=DATEDIF("5/16/1972",TODAY(),"m")
=DATEDIF("5/16/1972",TODAY(),"d")
11 | P a g e