﻿ ﻿Date and Time Formulas - Formulas and Functions - Excel 2016 All-in-One For Dummies (2016)

# Excel 2016 All-in-One For Dummies (2016)

### Date and Time Formulas

In This Chapter

Understanding dates and times in Excel

Creating formulas that calculate elapsed dates and times

Using the Date functions

Using the Time functions

Creating formulas that use dates and times can be a little confusing if you don’t have a good understanding of how Excel treats these types of values. After you’re equipped with this understanding, you can begin to make good use of the many Date and Time functions that the program offers.

This chapter begins with a quick overview of date and time numbers in Excel and how you can use them to build simple formulas that calculate differences between elapsed dates and times. The chapter goes on to survey Excel built-in Date and Time functions, including the Date functions that are available after you’ve installed and activated the Analysis ToolPak add-in.

Understanding Dates and Times

Excel doesn’t treat the dates and times that you enter in the cells of your worksheet as simple text entries. (For more information on inputting numbers in a spreadsheet, see Book II, Chapter 1.) Any entry with a format that resembles one of the date and time number formats utilized by Excel is automatically converted, behind the scenes, into a serial number. In the case of dates, this serial number represents the number of days that have elapsed since the beginning of the 20th century so that January 1, 1900, is serial number 1; January 2, 1900, is serial number 2; and so forth. In the case of times, this serial number is a fraction that represents the number of hours, minutes, and seconds that have elapsed since midnight, which is serial number 0.00000000, so that 12:00:00 p.m. (noon) is serial number 0.50000000; 11:00:00 p.m. is 0.95833333; and so forth.

As long as you format a numeric entry so that it conforms to a recognized date or time format, Excel enters it as a date or time serial number. Only when you enter a formatted date or time as a text entry (by prefacing it with an apostrophe) or import dates and times as text entries into a worksheet do you have to worry about converting them into date and time serial numbers, which enables you to build spreadsheet formulas that perform calculations on them.

Changing the Regional date settings

Excel isn’t set up to automatically recognize European date formats in which the number of the day precedes the number of the month and year. For example, you may want 6/11/2016 to represent November 6, 2016, rather than June 11, 2016. If you’re working with a spreadsheet that uses this type of European date system, you have to customize the Windows Regional settings for the United States so that the Short Date format in Windows programs, such as Excel and Word 2016, use the D/m/yyyy (day, month, year) format rather than the default M/d/yyyy (month, day, year) format.

When running Excel 2016 on Windows 10, you follow these steps:

1. Click the Windows Start button and then click Settings on the Start menu.

Windows 10 opens the Settings dialog box.

2. Click the Time & Language button in the Settings dialog box.

The Date and Time settings appear in the Settings dialog box.

3. Click the Change Date and Time formats link that appears under the Format examples that show you the current long and short date and time formatting.

The Settings dialog box displays drop-down text boxes where you can select new formatting for the short and long dates.

4. Click the Short Date drop-down button, click the dd-MMM-yy format at the bottom of the drop-down list, and then click the Close button.

If you’re running Excel 2016 on Windows 7 or 8, you do this by following these steps:

1. Open the Windows Control Panel in Category View.

To do this in Windows 8, from the Start screen, type con and then click the Control Panel item in the Search Results screen. To do this in Windows 7, you click the Start button on the Windows 7 taskbar followed by Control Panel on the Start menu.

The Control Panel window normally opens in Category view. If it is in Classic view, switch to Category view by choosing Category from the View By drop-down menu.

2. Click the Clock, Language, and Region link in the Control Panel window.

3. Click the Region link in the Windows 8 Control Panel window or the Regional and Language Options link in the Windows 7 Control Panel window.

The Region dialog box opens in Windows 8. The Regional and Language Options dialog box opens in Windows 7. In both dialog boxes, the Formats tab is selected.

4. Click the Additional Settings button.

The Customize Format dialog box opens.

5. Click the Date tab in the Customize Format dialog box.

6. Click the Short Date format and then type D/m/yyyy, the new date format.

You have to type this European date format because the United States regional settings don’t automatically include this format in the Short Date Style drop-down list. After manually entering this format, the European date format becomes part of the list that you can then select from in the future.

7. Click OK twice, once to close the Customize Format dialog box and then a second time to close the Region or Regional and Language Options dialog box.

8. Click the Close button in the upper-right corner of the Control Panel window or press Alt+F4 to close this window.

After changing the Short Date format in the Windows 10 Settings dialog box or the Windows 7 or 8 Control Panel, the next time you launch Excel 2016 it automatically formats dates à la European; so that, for example, 3/5/16 is interpreted as May 3, 2016, rather than March 5, 2016.

Don’t forget to change the Short Date format back to its original M/d/yyyy Short Date format for your version of Windows when working with spreadsheets that follow the “month-day-year” Short Date format preferred in the United States. Also, don’t forget that you have to restart Excel to get it to pick up on the changes that you make to any of the Windows date and time format settings.

Building formulas that calculate elapsed dates

Most of the date formulas that you build are designed to calculate the number of days or years that have elapsed between two dates. To do this, you build a simple formula that subtracts the earlier date from the later date.

For example, if you input the date 4/25/95 in cell B4 and 6/3/14 in cell C4 and you want to calculate the number of days that have elapsed between April 25, 1995, and June 3, 2014, in cell D4, you would enter the following subtraction formula in that cell:

=C4-B4

Excel then inputs 6979 as the number of days between these dates in cell D5 using the General number format.

If you want the result between two dates expressed in the number of years rather than the number of days, divide the result of your subtraction by the number of days in a year. In this example, you can enter the formula =D4/365 in cell E4 to return the result 19.12055, which you can then round off to 19 by clicking the Decrease Decimal button in the Number group on the Home tab of the Ribbon or by pressing Alt+H9 until only 19 remains displayed in the cell.

Building formulas that calculate elapsed times

Some spreadsheets require that formulas calculate the amount of elapsed time between a starting and an ending time. For example, suppose that you keep a worksheet that records the starting and stopping times for your hourly employees, and you need to calculate the number of hours and minutes that elapse between these two times in order to figure their daily and monthly wages.

To build a formula that calculates how much time has elapsed between two different times of the day, subtract the starting time of day from the ending time of day. For example, suppose that you enter a person’s starting time in cell B6 and ending time in C6. In cell D6, you would enter the following subtraction formula:

=C6-B6

Excel then returns the difference in cell D6 as a decimal value representing what fraction that difference represents of an entire day (that is, a 24-hour period). If, for example, cell B6 contains a starting time of 9:15 a.m. and cell C6 contains an ending time of 3:45 p.m., Excel returns the following decimal value to cell D6:

6:30 AM

To convert this time of day into its equivalent decimal number, you convert the time format automatically given to it to the General format (Ctrl+Shift+`), which displays the following result in cell D6:

0.270833

To convert this decimal number representing the fraction of an entire day into the number of hours that have elapsed, you simply multiply this result by 24 as in =D6*24, which gives you a result of 6.5 hours.

Using Date Functions

Excel contains a number of built-in Date functions that you can use in your spreadsheets. When you install and activate the Analysis ToolPak add-in (see Book I, Chapter 2 for details), you have access to a number of additional Date functions — many of which are specially designed to deal with the normal Monday through Friday, five-day workweek (excluding, of course, your precious weekend days from the calculations).

TODAY

The easiest Date function has to be TODAY. This function takes no arguments and is always entered as follows:

=TODAY()

When you enter the TODAY function in a cell by clicking it on the Date & Time command button’s drop-down list on the Ribbon’s Formulas tab or by typing it, Excel returns the current date by using the following Date format:

9/15/2016

Keep in mind that the date inserted into a cell with the TODAY function is not static. Whenever you open a worksheet that contains this function, Excel recalculates the function and updates its contents to the current date. This means that you don’t usually use TODAY to input the current date when you’re doing it for historical purposes (an invoice, for example) and never want it to change.

If you do use TODAY and then want to make the current date static in the spreadsheet, you need to convert the function into its serial number. You can do this for individual cells: First, select the cell, press F2 to activate Edit mode, press F9 to replace =TODAY() with today’s serial number on the Formula bar, and click the Enter button to insert this serial number into the cell. You can do this conversion on a range of cells by selecting the range, copying it to the Clipboard by clicking the Copy button on the Home tab of the Ribbon (or pressing Ctrl+C), and then immediately pasting the calculated values into the same range by choosing the Paste Values option from the Paste command button’s drop-down menu (or pressing Alt+HVV).

DATE and DATEVALUE

The DATE function on the Date & Time command button’s drop-down menu returns a date serial number for the date specified by the year, month, and day argument. This function uses the following syntax:

DATE(year,month,day)

This function comes in handy when you have a worksheet that contains the different parts of the date in separate columns, similar to the one shown in Figure 3-1. You can use it to combine the three columns of date information into a single date cell that you can use in sorting and filtering. (See Book VI, Chapters 1 and 2 to find out how to sort and filter data.)

Figure 3-1: Using the DATE function to combine separate date information into a single entry.

The DATEVALUE function on the Date & Time button’s drop-down menu on the Formulas tab returns the date serial number for a date that’s been entered into the spreadsheet as text so that you can use it in date calculations. This function takes a single argument:

DATEVALUE(date_text)

Suppose, for example, that you’ve made the following text entry in cell B12:

'5/21/2016

(Remember that when you preface an entry with an apostrophe, Excel inserts that entry as text even if the program would otherwise put it in as a value.) You can then convert this text entry into a date serial number by entering the following formula in cell C12 next door:

=DATEVALUE(B12)

Excel then returns the date serial number, 42511, to cell C12, which you can convert into a more intelligible date by formatting it with one of Excel’s Date number formats (Ctrl+1).

You must convert the DATE and DATEVALUE functions into their calculated date serial numbers in order to sort and filter them. To convert these functions individually, select a cell, press F2 to activate Edit mode, and then press F9 to replace the function with the calculated date serial number; finally, click the Enter button on the Formula bar to insert this serial number into the cell. To do this conversion on a range of cells, select the range, copy it to the Clipboard by pressing Ctrl+C, and then immediately paste the calculated serial numbers into the same range by choosing the Paste Values option from the Paste command button’s drop-down menu (or press Alt+HVV).

DAY, WEEKDAY, MONTH, and YEAR

The DAY, WEEKDAY, MONTH, and YEAR Date functions on the Date & Time command button’s drop-down menu all return just parts of the date serial number that you specify as their argument:

· DAY(serial_number) to return the day of the month in the date (as a number between 1 and 31).

· WEEKDAY(serial_number,[return_type]) to return the day of the week (as a number between 1 and 7 or 0 and 6). The optional return_type argument is a number between 1 and 3; 1 (or no return_type argument) specifies the first type where 1 equals Sunday and 7 equals Saturday; 2 specifies the second type where 1 equals Monday and 7 equals Sunday; and 3 specifies the third type where 0 equals Monday and 6 equals Sunday.

· MONTH(serial_number) to return the number of the month in the date serial number (from 1 to 12).

· YEAR(serial_number) to return the number of the year (as an integer between 1900 and 9999) in the date serial number.

For example, if you enter the following DAY function in a cell as follows:

DAY(DATE(16,4,15))

Excel returns the value 15 to that cell. If, instead, you use the WEEKDAY function as follows:

WEEKDAY(DATE(16,4,15))

Excel returns the value 7, which represents Saturday (using the first return_type where Sunday is 1 and Saturday is 7) because the optional return_type argument isn’t specified. If you use the MONTH function on this date as in the following:

MONTH(DATE(16,4,15))

Excel returns 4 to the cell. If, however, you use the YEAR function on this date as in the following:

YEAR(DATE(16,4,15))

Excel returns 1916 to the cell (instead of 2016).

This means that if you want to enter a year in the 21st century as the year argument of the DATE function, you need to enter all four digits of the date, as in the following:

DATE(2016,4,15)

Note that you can use the YEAR function to calculate the difference in years between two dates. For example, if cell B6 contains 7/23/1978 and cell C6 contains 7/23/2016, you can enter the following formula using the YEAR function to determine the difference in years:

=YEAR(C6)-YEAR(B6)

Excel then returns 38 to the cell containing this formula.

Don’t use these functions on dates entered as text entries. Always use the DATEVALUE function to convert these text dates and then use the DAY, WEEKDAY, MONTH, or YEAR functions on the serial numbers returned by the DATEVALUE function to ensure accurate results.

DAYS360

The DAYS360 function on the Date & Time command button’s drop-down menu returns the number of days between two dates based on a 360-day year (that is, one in which there are 12 equal months of 30 days each). The DAYS360 function takes the following arguments:

DAYS360(start_date,end_date,[method])

The start_date and end_date arguments are date serial numbers or references to cells that contain such serial numbers. The optional method argument is either TRUE or FALSE, where FALSE specifies the use of the U.S. calculation method and TRUE specifies the use of the European calculation method:

· U.S. (NASD) method (FALSE or method argument omitted): In this method, if the starting date is equal to the 31st of the month, it becomes equal to the 30th of the same month; if the ending date is the 31st of a month and the starting date is earlier than the 30th of the month, the ending date becomes the 1st of the next month; otherwise, the ending date becomes equal to the 30th of the same month.

· European method (TRUE): In this method, starting and ending dates that occur on the 31st of a month become equal to the 30th of the same month.

Other special Date functions

Excel includes other special Date functions in the Date and Time category in the Insert Function dialog box. These particular Date functions expand your abilities to do date calculations in the worksheet — especially those that work only with normal workdays, Monday through Friday.

EDATE

The EDATE (for Elapsed Date) function calculates a future or past date that is so many months ahead or behind the date that you specify as its start_date argument. You can use the EDATE function to quickly determine the particular date at a particular interval in the future or past (for example, three months ahead or one month ago).

The EDATE function takes two arguments:

EDATE(start_date,months)

The start_date argument is the date serial number that you want used as the base date. The months argument is a positive (for future dates) or negative (for past dates) integer that represents the number of months ahead or months past to calculate.

For example, suppose that you enter the following EDATE function in a cell:

=EDATE(DATE(2016,1, 31),1)

Excel returns the date serial number, 42429, which becomes 2/29/2016 when you apply the first Date format to its cell.

EOMONTH

The EOMONTH (for End of Month) function calculates the last day of the month that is so many months ahead or behind the date that you specify as its start_date argument. You can use the EOMONTH function to quickly determine the end of the month at a set interval in the future or past.

For example, suppose that you enter the following EOMONTH function in a cell:

=EOMONTH(DATE(2016,1,1),1)

Excel returns the date serial number, 42429, which becomes 2/29/2016 when you apply the first Date format to its cell.

NETWORKDAYS

The NETWORKDAYS function returns the number of workdays that exist between a starting date and ending date that you specify as arguments:

NETWORKDAYS(start_date,end_date,[holidays])

When using this function, you can also specify a cell range in the worksheet or array constant to use as an optional holidays argument that lists the state, federal, and floating holidays observed by your company. Excel then excludes any dates listed in the holidays argument when they occur in between start_date and end_date arguments.

Figure 3-2 illustrates how this function works. In this worksheet, I created a list in the cell range B3:B13 with all the observed holidays in the calendar year 2016. I then entered the following NETWORKDAYS function in cell E4:

NETWORKDAYS(DATE(2015,12,31),DATE(2016,12,31),B3:B13)

Figure 3-2: Using the NETWORKDAYS function to find the number of workdays between two dates.

The preceding function calculates the number of workdays between December 31, 2015, and December 31, 2016 (262 total work days), and then subtracts the dates listed in the cell range B3:B13 if they fall on a weekday. As 9 of the 11 holidays in the range B3:B13 happen to fall on a weekday in the year 2016, the number of workdays between December 31, 2015, and December 31, 2016, is calculated as 253 in cell E4 (262-9=253).

WEEKNUM

The WEEKNUM function returns a number indicating where the week in a particular date falls within the year. This function takes the following arguments:

WEEKNUM(serial_number,[return_type])

In this function, the serial_number argument is the date whose week in the year you want to determine. The optional return_type argument is number 1 or 2, where number 1 (or omitted) indicates that the new week begins on Sunday and weekdays are numbered from 1 to 7. Number 2 indicates that the new week begins on Monday and that weekdays are also numbered from 1 to 7.

For example, if you enter the following WEEKNUM function in a cell:

=WEEKNUM(DATE(2016,1,17))

Excel returns the number 4, indicating that the week containing the date January 17, 2016, is the fourth week in the year when the Sunday is considered to be the first day of the week. (January 17, 2016, falls on a Sunday.) Note that if I had added 2 as the optional return-type argument, Excel would return 3 as the result because January 17, 2016, is deemed to fall on the last day of the third week of the year when Monday is considered the first day of the week.

WORKDAY

You can use the WORKDAY function to find out the date that is so many workdays before or after a particular date. This function takes the following arguments:

WORKDAY(start_date,days,[holidays])

The start_date argument is the initial date that you want used in calculating the date of the workday that falls so many days before or after it. The days argument is the number of workdays ahead (positive integer) or behind (negative integer) the start_date. The optional holidays argument is an array constant or cell range that contains the dates of the holidays that should be excluded (when they fall on a weekday) in calculating the new date.

For example, suppose that you want to determine a due date for a report that is 30 workdays after February 1, 2016, by using the same holiday schedule entered in the cell range B3:B13 in the Work Days 2016 workbook, shown in Figure 3-2. To do this, you enter the following formula:

=WORKDAY(DATE(2016,2,1),30,B3:B11)

Excel then returns the serial number 42444 to the cell, which then appears as March 15, 2016 (two days before St. Patrick’s Day), when you format it with the Short Date format.

YEARFRAC

The YEARFRAC (for Year Fraction) function enables you to calculate the fraction of the year, which is computed from the number of days between two dates. You can use the YEARFRAC function to determine the proportion of a whole year’s benefits or obligations to assign to a specific period.

The YEARFRAC function uses the following arguments:

YEARFRAC(start_date,end_date,[basis])

The optional basis argument in the YEARFRAC function is a number between 0 and 4 that determines the day count basis to use in determining the fractional part of the year:

· 0 (or omitted) to base it on the U.S. (NASD) method of 30/360 (see “DAYS360” earlier in the chapter for details on the U.S. method)

· 1 to base the fraction on actual days/actual days

· 2 to base the fraction on actual days/360

· 3 to base the fraction on actual days/365

· 4 to base the fraction on the European method of 30/360 (see “DAYS360” earlier in the chapter for details on the European method)

For example, if you enter the following YEARFRAC formula in a cell to find what percentage of the year remains as of October 15, 2016:

=YEARFRAC(DATE(2016,10,15),DATE(2016,12,31))

Excel returns the decimal value 0.2111111 to the cell, indicating that just over 21 percent of the year remains.

Using Time Functions

Excel offers far fewer Time functions when compared with the wide array of Date functions. Like the Date functions, however, the Time functions enable you to convert text entries representing times of day into time serial numbers so that you can use them in calculations. The Time functions also include functions for combining different parts of a time into a single serial time number, as well as those for extracting the hours, minutes, and seconds from a single time serial number.

NOW

The NOW function on the Date & Time command button’s drop-down menu gives you the current time and date based on your computer’s internal clock. You can use the NOW function to date- and time-stamp the worksheet. Like the TODAY function, NOW takes no arguments and is automatically recalculated every time you open the spreadsheet:

=NOW()

When you enter the NOW function in a cell, Excel puts the date before the current time. It also formats the date with the first Date format and the time with the 24-hour Time format. So, if the current date were August 19, 2016, and the current time was 12:57 p.m. at the moment when Excel calculates the NOW function, your cell would contain the following entry:

8/19/2016 12:57

Note that the combination Date/Time format that the NOW function uses is a custom number format. If you want to assign a different date/time to the date and time serial numbers returned by this function, you have to create your own custom number format and then assign it to the cell that contains the NOW function. (See Book II, Chapter 2 for information on creating custom number formats.)

TIME and TIMEVALUE

The TIME function on the Date & Time command button’s drop-down menu enables you to create a decimal number representing a time serial number, ranging from 0 (zero) to 0.99999999, representing time 0:00:00 (12:00:00 AM) to 23:59:59 (11:59:59 PM). You can use the TIME function to combine the hours, minutes, and seconds of a time into a single time serial number when these parts are stored in separate cells.

The TIME function takes the following arguments:

TIME(hour,minute,second)

When specifying the hour argument, you use a number between 0 and 23. (Any number greater than 23 is divided by 24, and the remainder is used as the hour value.) When specifying the minute and second arguments, you use a number between 0 and 59. (Any minute argument greater than 59 is converted into hours and minutes, just as any second argument greater than 59 is converted into hours, minutes, and seconds.)

For example, if cell A3 contains 4, cell B3 contains 37, and cell C3 contains 0, and you enter the following TIME function in cell D3:

=TIME(A3,B3,C3)

Excel enters 4:37 AM in cell D3. If you then assign the General number format to this cell (Ctrl+Shift+` or Ctrl+~), it would then contain the time serial number, 0.192361.

The TIMEVALUE function converts a time entered or imported into the spreadsheet as a text entry into its equivalent time serial number so that you can use it in time calculations. The TIMEVALUE function uses a single time_text argument as follows:

TIMEVALUE(time_text)

So, for example, if you put the following TIMEVALUE function in a cell to determine the time serial number for 10:35:25:

=TIMEVALUE("10:35:25")

Excel returns the time serial number 0.441262 to the cell. If you then assign the first Time number format to this cell, the decimal number appears as 10:35:25 a.m. in the cell.

HOUR, MINUTE, and SECOND

The HOUR, MINUTE, and SECOND functions on the Date & Time command button’s drop-down menu enable you to extract specific parts of a time value in the spreadsheet. Each of these three Time functions takes a single serial_number argument that contains the hour, minute, or second that you want to extract.

So, for example, if cell B5 contains the time 1:30:10 p.m. (otherwise known as serial number 0.5626157) and you enter the following HOUR function in cell C5:

=HOUR(B5)

Excel returns 13 as the hour to cell C5 (hours are always returned in 24-hour time). If you then enter the following MINUTE function in cell D5:

=MINUTE(B5)

Excel returns 30 as the number of minutes to cell D5. Finally, if you enter the following SECOND function in cell E5:

=SECOND(B5)

Excel returns 10 as the number of seconds to cell E5.

﻿