You are on page 1of 11

Data and Computing Skills

Week 3.
Using Formulas with Dates and Times

Understanding How Excel Handles Dates and Times


To Excel, a date is simply a number. More precisely, a date is a serial number that represents the
number of days since the fictitious date of January 0, 1900. A serial number of 1 corresponds to
January 1, 1900; a serial number of 2 corresponds to January 2, 1900; and so on. This system
makes it possible to create formulas that perform calculations with dates. For example, you can
create a formula to calculate the number of days between two dates (just subtract one from the
other).
Excel supports dates from January 1, 1900, through December 31, 9999 (serial number =
2958465).

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

Entry Excel Interpretation


11:30 AM 11:30 AM
11:30:00 AM 11:30 AM
11:30 PM 11:30 PM
11:30 11:30 AM
13:30 1:30 PM

Formatting dates and times


You have a great deal of flexibility in formatting cells that contain dates and times. For example,
you can format the cell to display the date part only, the time part only, or both the date and time
parts.
You format dates and times by selecting the cells and then using the Number tab of the Format
Cells dialog box. To display this dialog box, click the dialog box launcher icon in the Number
group of the Home tab, or click the Number Format control and choose More Number Formats
from the list that appears.
The Date category shows built--in date formats, and the Time category shows built--in time for-
mats. Some formats include both date and time displays. Just select the desired format from the
Type list and then click OK.

Problems with dates


Excel has some problems when it comes to dates. Many of these problems stem from the fact
that Excel was designed many years ago. Excel designers basically emulated the Lotus 1--2--3
program’s limited date and time features, which contain a nasty bug that was duplicated
intentionally in Excel (described next). If Excel were being designed from scratch today, I’m sure
it would be much more versatile in dealing with dates. Unfortunately, users are currently stuck
with a product that leaves much to be desired in the area of dates.

Excel’s leap year bug


A leap year, which occurs every four years, contains an additional day (February 29). Specifically,
years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by
400. Although the year 1900 was not a leap year, Excel treats it as such. In other words, when
you type 2/29/1900 into a cell, Excel interprets it as a valid date and assigns a serial number of
60.

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

Inconsistent date entries


You need to be careful when entering dates by using two digits for the year. When you do so,
Excel has some rules that determine which century to use.
Two--digit years between 00 and 29 are interpreted as 21st--century dates, and two--digit years
between 30 and 99 are interpreted as 20th--century dates. For example, if you enter 12/15/28,
Excel interprets your entry as December 15, 2028. But if you enter 12/15/30, Excel sees it as
December 15, 1930, because Windows uses a default boundary year of 2029. You can keep the
default as is or change it via the Windows Control Panel. From the Region dialog box, click the
Additional Settings button to display the Customize Format dialog box. Select the Date tab and
then specify a different year.

Using Excel’s Date and Time Functions


Excel has quite a few functions that work with dates and times. These functions are accessible
by choosing Formulas ➪ Function Library ➪ Date & Time.

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.

Getting the current date and time


Instead of typing the current date and time, you can use one of two Excel functions. The TODAY
function returns the current date:
=TODAY()

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.

Here are a few Excel DATE formula examples:


=DATE(2015, 5, 20) - returns a serial number corresponding to 20-May-2015.

=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.

=YEAR("20-May-2015") - returns the year of the specified date.


=YEAR(DATE(2015,5,20)) - a more reliable method to get the year of a given date.

6|P a g e
Data and Computing Skills

=YEAR(TODAY()) - returns the current year.

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.

Extracting parts of a date


Although it may seem trivial, it’s often helpful to pick out a specific part of a date. For example,
you may need to filter all records that have order dates within a certain month or all employees
who have time allocated to Saturdays. In these situations, you would need to pull out the month
and workday number from the formatted dates.
Excel provides a simple set of functions to parse dates out into their component parts. These
functions are as follows:
▪ YEAR extracts the year from a given date.
▪ MONTH extracts the month from a given date.

7|P a g e
Data and Computing Skills

▪ DAY extracts the month day number from a given date.


▪ WEEKDAY returns the weekday number for a given date.
▪ WEEKNUM returns the week number for a given date.
Figure 3 demonstrates the use of these functions to parse the date in cell C3 into its com-ponent
parts.

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)

Calculating the number of workdays between two dates


Oftentimes when reporting on the elapsed number of days between a start date and end date,
it’s not appropriate to count the weekends in the final number of days. Operations are
typically shut down on the weekends, so you would want to avoid counting those days.
You can use Excel’s NETWORKDAYS function to calculate the number of days between a
start date and end date excluding weekends.
As you can see in Figure 1, the NETWORKDAYS function is used in cell E4 to calculate the
number of workdays between 1/1/2022 and 12/31/2022.

Figure 1. Calculating the number of workdays between two dates

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")

You can, of course, reference a cell that contains a date:


=DATEDIF(B4,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

Code What It Returns


"y" The number of complete years in the period.
"m" The number of complete months in the period.
"d" The number of days in the period.
The difference between the days in start_date and
"md"
end_date. The months and years of the dates are ignored.
The difference between the months in start_date and
"ym"
end_date. The days and years of the dates are ignored.
The difference between the days of start_date and
"yd"
end_date. The years of the dates are ignored.

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

You might also like