Содержание
- Add or subtract time
- Add time
- Subtract time
- Add time
- Subtract time
- How to Work With Date and Time Formulas in Excel
- Excel Date and Time Formulas (Quick Video Tutorial)
- Typing Dates and Times in Excel
- 1. How to Type Dates
- 2. How to Type Times
- 3. How to Type Date-Time Together
- Bonus: Excel Keyboard Shortcut for Current Time
- Formatting Dates in Excel
- Format Excel Cells
- Get Data From Dates and Times
- Find the Difference Between Dates and Times
- 1. Days Between Dates
- 2. Months Between Dates
- 3. Years Between Dates
- Bonus: Work Days Between Dates
- Recap and Keep Learning
Add or subtract time
Let’s say that you need to add two different time values together to get a total. Or, you need to subtract one time value from another to get the total time spent working on a project.
As you’ll see in the sections below, Excel makes it easy to add or subtract time.
Add time
Suppose that you want to know how many hours and minutes it will take to complete two tasks. You estimate that the first task will take 6 hours and 45 minutes and the second task will take 9 hours and 30 minutes.
Here is one way to set this up in the a worksheet.
Enter 6:45 in cell B2, and enter 9 :30 in cell B3.
In cell B4, enter =B2+B3 and then press Enter.
The result is 16:15—16 hours and 15 minutes—for the completion the two tasks.
Tip: You can also add up times by using the AutoSum function to sum numbers. Select cell B4, and then on the Home tab, choose AutoSum. The formula will look like this: =SUM(B2:B3). Press Enter to get the same result, 16 hours and 15 minutes.
Well, that was easy enough, but there’s an extra step if your hours add up to more than 24. You need to apply a special format to the formula result.
To add up more than 24 hours:
In cell B2 type 12:45, and in cell B3 type 15:30.
Type =B2+B3 in cell B4, and then press Enter.
The result is 4:15, which is not what you might expect. This is because the time for Task 2 is in 24-hour time. 15:30 is the same as 3:30.
To display the time as more than 24 hours, select cell B4.
On the Home tab, in the Cells group, choose Format, and then choose Format Cells.
In the Format Cells box, choose Custom in the Category list.
In the Type box, at the top of the list of formats, type [h]:mm;@ and then choose OK.
Take note of the colon after [h] and a semicolon after mm.
The result is 28 hours and 15 minutes. The format will be in the Type list the next time you need it.
Subtract time
Here’s another example: Let’s say that you and your friends know both your start and end times at a volunteer project, and want to know how much time you spent in total.
Follow these steps to get the elapsed time—which is the difference between two times.
In cell B2, enter the start time and include “ a” for AM or “ p” for PM. Then press Enter.
In cell C2, enter the end time, including “ a” or “ p” as appropriate, and then press Enter.
Type the other start and end times for your friends, Joy and Leslie.
In cell D2, subtract the end time from the start time by entering the formula =C2-B2, and then press Enter.
In the Format Cells box, click Custom in the Category list.
In the Type list, click h:mm (for hours and minutes), and then click OK.
Now we see that Richard worked 3 hours and 45 minutes.
To get the results for Joy and Leslie, copy the formula by selecting cell D2 and dragging to cell D4.
The formatting in cell D2 is copied along with the formula.
To subtract time that’s more than 24 hours:
It is necessary to create a formula to subtract the difference between two times that total more than 24 hours.
Follow the steps below:
Referring to the above example, select cell B1 and drag to cell B2 so that you can apply the format to both cells at the same time.
In the Format Cells box, click Custom in the Category list.
In the Type box, at the top of the list of formats, type m/d/yyyy h:mm AM/PM.
Notice the empty space at the end of yyyy and at the end of mm.
The new format will be available when you need it in the Type list.
In cell B1, type the start date, including month/day/year and time using either “ a” or “ p” for AM and PM.
In cell B2, do the same for the end date.
In cell B3, type the formula =(B2-B1)*24.
The result is 31.5 hours.
Note: You can add and subtract more than 24 hours in Excel for the web but you cannot apply a custom number format.
Add time
Suppose you want to know how many hours and minutes it will take to complete two tasks. You estimate that the first task will take 6 hours and 45 minutes and the second task will take 9 hours and 30 minutes.
In cell B2 type 6:45, and in cell B3 type 9:30.
Type =B2+B3 in cell B4, and then press Enter.
It will take 16 hours and 15 minutes to complete the two tasks.
Tip: You can also add up times using AutoSum. Click in cell B4. Then click Home > AutoSum. The formula will look like this: =SUM(B2:B3). Press Enter to get the result, 16 hours and 15 minutes.
Subtract time
Say you and your friends know your start and end times at a volunteer project, and want to know how much time you spent. In other words, you want the elapsed time or the difference between two times.
In cell B2 type the start time, enter a space, and then type “ a” for AM or “ p” for PM, and press Enter. In cell C2, type the end time, including “ a” or “ p” as appropriate, and press Enter. Type the other start and end times for your friends Joy and Leslie.
In cell D2, subtract the end time from the start time by typing the formula: =C2-B2, and then pressing Enter. Now we see that Richard worked 3 hours and 45 minutes.
To get the results for Joy and Leslie, copy the formula by clicking in cell D2 and dragging to cell D4. The formatting in cell D2 is copied along with the formula.
Источник
How to Work With Date and Time Formulas in Excel
If you use Excel regularly, I’m sure you’ve come across dates and times in your cells. Data often has a record of when it was created or updated, so knowing how to work with this data is essential.
Here are three key skills that you’ll learn in this tutorial:
- How to format dates in Excel so that they appear in your preferred style
- Formulas to calculate the number of days, months, and years between two dates
- An Excel date formula to log today’s date, and a keyboard shortcut to add the current time
Microsoft Excel can basically do anything with data, if you just know how. This tutorial is another key step to adding skills to your Excel toolbelt. Let’s get started.
Excel Date and Time Formulas (Quick Video Tutorial)
This screencast will walk you through how to work with dates and times in Excel. I cover formatting dates to different styles, as well as Excel date formulas to calculate and work with dates. Make sure to download the free Excel workbook with exercises that I’ve attached to this tutorial.
Keep reading for a written reference guide on how to format dates and times in Excel, and work with them in your formulas. I’ll even share several tips that weren’t covered in the screencast.
Typing Dates and Times in Excel
For this part of the tutorial, use the tab titled «Typing Dates & Times» in the example workbook.
One of the keys to working with dates and times in Excel is capturing the data correctly. Here’s how to type dates and times in your Excel spreadsheets:
1. How to Type Dates
I recommend typing dates in the same format that your system uses. For our American readers, a full date would be in the «day/month/year» format. European style dates are «month/day/year.»
When I’m typing dates, I always type in the full date with the month, day and year. If I only want to show the month and the year, I’ll simply format it that way (more on that in a minute.)
2. How to Type Times
It’s easy to type times in Excel. We can specify anything from just an hour of the day, to the exact second that something took place.
If I wanted to log the time as 4PM, I’d type «4 pm» into a cell in Excel and then press enter:
Notice once we press enter, Excel converts what we’ve typed into a hours : minutes: seconds data format.
Here’s how to log a more specific time in your spreadsheet:
The key is to use colons to separate the section of the time data, and then add a space plus «AM» or «PM.»
3. How to Type Date-Time Together
You can also type combinations of dates and times in Excel for highly specific timestamps.
To type a date-time combination, simply use what we’ve already learned about typing dates, and typing times.
Notice that Excel has converted the time to a 24 hour format when it’s used in conjunction with a date, by default. If you want to change the style of this date, keep reading.
Bonus: Excel Keyboard Shortcut for Current Time
One of my favorite Excel keyboard shortcuts inserts the current time into a spreadsheet. I use this formula often, when I’m noting the time I made a change to my data. Try it out:
Formatting Dates in Excel
For this part of the tutorial, use the tab titled «Formatting Dates & Times» in the example workbook.
What can you do when your dates are European style dates? That is, they’re in a day-month-year format, and you need to convert them to the more familiar month-day-year format?
All of these cells contain exactly the same data, they’re just formatted in different ways.
In the screenshot above, what might surprise you is that all six of those cells contain exactly the same data — «1/22/2017.» What differs is how they’re formatted in Excel. The original data is identical, but it can be formatted to show in a variety of ways.
In most cases, it’s better to use formatting to modify the style of our dates. We don’t need to modify the data itself — just change how it’s presented.
Format Excel Cells
To change the appearance of our date and time data, make sure that you’re working on the Home tab of Excel. On the Ribbon (menu at the top of Excel), find the section labeled Number.
There’s a small arrow in the lower right corner of the section. Click it to open the Format Cells menu.
To format cells in Excel with built in styles, make sure you’re working on the Home tab and click the dropdown arrow next to the word «Number» in this screenshot.
The Format Cells menu has a variety of options for styling your dates and times. You could turn «1/22/2017» into «Sunday, January 22nd» with just formatting. Then, you could grab the format painter and change all of your cell styles.
The Format Cells menu allows you to change the styles of your dates and times without the work of changing the original date.
Spend some time exploring this menu and trying out the different styles for your Excel dates and times.
Get Data From Dates and Times
Let’s say that we have a list of data that has very specific dates and times, and we want to get simpler versions of those formulas. Maybe we have a list of exact transaction dates, but we want to work with them at a higher level, grouping them by year or month.
You can get the year from a date with this Excel formula:
To get just the month from a date cell, use the following Excel formula:
Find the Difference Between Dates and Times
For this part of the tutorial, use the tab titled «DATEDIF» in the example workbook.
While formats are used to change how dates and times are presented, formulas in Excel are used to modify, calculate, or work with dates and times programatically.
The DATEDIF formula is powerful for calculating differences between days. Give the formula two dates and and it will return the number of days, months, and years between two dates. Let’s look at how to use it.
1. Days Between Dates
This Excel date formula will calculate the number of days between two dates:
The formula takes two cells, separated by commas, and then uses a «d» to calculate the difference in days.
The DATEDIF formula takes two date cells and calculates the days between them.
Here are some ideas for how you could use this Excel date formula to your advantage:
- Calculate the difference between today and your birthday to start a birthday countdown
- Use a DATEDIF to calculate the difference between two dates and divide your stock portfolio’s growth by the number of days to calculate the growth (or loss!) per day
2. Months Between Dates
DATEDIF also calculates the number of months between two dates. This date formula in Excel is very similar, but substitutes an «m» for «d» to calculate the difference in months:
However, there’s a quirk in the way Excel applies DATEDIF: it calculates whole months between dates. See the screenshot below.
To me, there are three months between January 1st and March 31st (all of January, all of February, and almost all of March.) However, because Excel uses whole months, it only considers January and February as completed, whole months, so the result is «2.»
Here’s my preferred way to calculate the number of months between two dates. We’ll find the date difference in days, and then divide it by the average number of days in a month — 30.42 .
Let’s apply our modified DATEDIF to two dates:
Much better. The output of 2.99 is very close to 3 full months, and this will be much more useful in future formulas.
The official Excel documentation has a complex method to calculate months between dates, but this is a simple and easy way to get it pretty close. Writing a good Excel formula is about finding the sweet spot of precision and simplicity, and this formula does both.
3. Years Between Dates
Finally, let’s calculate the number of years between two dates. The official way to calculate years between dates is with the following formula:
Notice that this is the same as our past DATEDIF formulas, but we’ve simply substituted the last part of the formula with «y» to calculate the number of years between two dates. Let’s see it in action:
Notice that this works like the DATEDIF for months: it counts only full years that have passed. I’d rather include partial years passing as well. Here’s a better DATEDIF for years:
Basically, we’re just getting the date difference in days, and then dividing it by 365 to calculate it as a year. Here’s the results:
DATEDIF is extremely powerful, but watch out for how it works: it’s going to only calculate full months or years that have passed by default. Use my modified versions for more precision in the results.
Bonus: Work Days Between Dates
The Excel date formulas covered above focus on the number of business days between dates. However, it’s sometimes helpful to just calculate the number of workdays (basically weekdays) between two dates.
In this case, we’ll use =NETWORKDAYS to calculate the number of workdays between two dates.
In the screenshot below, I show an example of using NETWORKDAYS. You can see the calendar showing how the formula calculated a result of «4.»
If you have known holidays in the timeframe that you want to exclude, check out the official NETWORKDAYS documentation.
Recap and Keep Learning
Dates and times are ubiquitous to spreadsheets. Excel date formulas and formatting options are helpful. The techniques in this tutorial can take your Excel skills to the next level so that you can incorporate date-driven data seamlessly into your spreadsheets.
You’ve added one skill to your Excel toolbox — why stop here? Chain Excel formulas and skills to create powerful spreadsheets. To keep learning more about working with Excel spreadsheets, check out these other resources:
- IF Statements are logic built into your spreadsheet that show results based on conditions. You could combine an IF Statement with a date range to show data based on a date or time. Check out this tutorial to learn them.
- You could combine the VLOOKUP tutorial with date and time formulas to match values based on a date or time.
- Find and Remove Duplicates is an Excel function used in combination with date and time data, as it’s often one of the best bits of data to check for duplicates.
How do you work with calendar and time data in your Excel spreadsheets? Are there are any Excel date or time formulas that I’m missing from this tutorial that you use regularly? If so, let me know in the comments.
Источник
Dates and times are two of the most common data types people work with in Excel, but they are also possibly the most frustrating to work with, especially if you are new to Excel and still learning. This is because Excel uses a serial number to represent the date instead of a proper month, day, or year, nevermind hours, minutes, or seconds. It’s made more complicated by the fact that dates are also days of the week, like Monday or Friday, even though Excel doesn’t explicitly store that information in the cells. Here is the definitive guide to working with dates and times in Excel…
How Excel Stores Dates
The source of most of the confusion around dates and times in Excel comes from the way that the program stores the information. You’d expect it to remember the month, the day, and the year for dates, but that’s not how it works…
Excel stores dates as a serial number that represents the number of days that have taken place since the beginning of the year 1900. This means that January 1, 1900 is really just a 1. January 2, 1900 is 2. By the time we get all the way to the present decade, the numbers have gotten pretty big… September 10, 2013 is stored as 41527.
Importantly, any date before January 1, 1900 is not recognized as a date in Excel. There are no “negative” date serial numbers on the number line.
It seems confusing, but it makes it a lot easier to add, subtract, and count days. A week from September 10, 2013 (September 17, 2013), is just 41527 + 7 days, or 41534.
How Excel Stores Times
Excel stores times using the exact same serial numbering format as with dates. Days start at midnight (12:00am or 0:00 hours). Since each hour is 1/24 of a day, it is represented as that decimal value: 0.041666…
That means that 9:00am (09:00 hours) on September 10, 2013 will be stored as 41527.375.
When a time is specified without a date, Excel stores it as if it occurred on January 0, 1900. In other words, 3:00pm (15:00 hours) is stored as 0.625. This can make doing math for time-only values (that have no date) challenging, since subtracting 6 hours (6:00) from 3:00am (03:00 hours) will become negative and count as an error: 0.125 – 0.25 = -0.125, which is displayed as #########.
Minutes and seconds in Excel work the same way as hours…
A minute is 1/60 of an hour, which is 1/24 of a day, or 1/1440 of a day in total, which calculates to 0.00069444…
A second is 1/60 of an minute, which is 1/60 of an hour, which is 1/24 of a day, or 1/86400 of a day in total, which calculates to 0.00001157407…
Working with Dates and Times
DATE() and TIME()
Serial numbers aren’t all that intuitive to use. Fortunately, Excel has a set of functions to make it easier to find and use dates and times, starting with DATE and TIME. The syntax is as follows:
=DATE(year, month, day)
=TIME(hours, minutes, seconds)
For both functions, specify the year, month, and day, or hours, minutes, and seconds as numbers. For example, September 10, 2013 can be entered as:
=DATE(2013,9,10)
It will be stored as 41527, which means that it is technically storing 12:00am on September 10, 2013.
For times, 6:00pm (18:00 hours) can be entered as:
=TIME(18,0,0)
It will be stored as 0.75, which means that it is technically storing 6:00pm on January 0, 1900.
If we want to represent a specific time and date, we can add the two functions together. For example, 6:00pm (18:00 hours) on September 10, 2013 can be entered as:
=DATE(2013,9,10)+TIME(18,0,0)
It will be calculate as 41527.75, which means Excel is storing exactly the date we want…
Additional Date and Time Setting Functions
Excel has a few additional functions to make declaring dates easier.
TODAY()
The TODAY function always returns the current date’s serial number. The TODAY function is just entered as:
=TODAY()
This article was written at 6:30pm (18:30 hours) on September 24, 2013, and the TODAY function calculated to 41541. That means that it is technically storing 12:00am on September 24, 2013.
NOW()
A similar function called NOW always returns the current date and time’s serial number. The NOW function is just entered as:
=NOW()
Again, at 6:30pm (18:30 hours) on September 24, 2013, the function calculated to 41541.77081333… NOW stores the exact time and date, down to the second.
EDATE() and EOMONTH()
The EDATE function gives the date the specified number of months away from the input date. The EOMONTH function gives the date of the last day of the month. It can do so for the current month or a number of months in the future or the past. The syntax for each is as follows:
=EDATE(start_date, months)
=EOMONTH(start_date, months)
The start_date can be any date-formatted cell reference or date serial number.
The months field can be any number, though only the integer value will be used (e.g. it treats 2.8 as 2).
The EDATE and EOMONTH functions strip the time value from the date. For example, For example, if cell A1 stores September 10, 2013, we can get the value 2 months ahead as follows:
=EDATE(A1,2)
Returns 12:00am (0:00 hours) on November 10, 2013, or 41588. This function works even though the months have different numbers of days (September and November have 30, October has 31).
=EOMONTH(A1,2)
Returns 12:00am (0:00 hours) on November 30, 2013, or 41588. Again, this function works even though the months have different numbers of days.
WORKDAY()
Occasionally, it may be useful to count ahead based on work-days (Monday-Friday) instead of all 7 days of the week… For that, Excel has provided WORKDAY. The syntax for WORKDAY is as follows:
=WORKDAY(start_date, days, [holidays])
The start_date is as above.
The days input is the number of workdays ahead (or behind) of the present day you would like to move.
The [holidays] input is optional, but lets you disqualify specific days (like Thanksgiving or Christmas, for example), which might otherwise fall during the work week. These are date serial numbers provided in an array bounded by brackets: { }. To specify multiple holidays, the dates must be held in cells – it is not possible to put multiple DATE functions in an array.
For example, let’s find the date 6 work days before 6:00pm (18:00 hours) on September 10, 2013 (stored in cell A1). Monday, September 2nd is Labor Day, so let’s include that as a holiday:
=WORKDAY(A1,-6,DATE(2013,9,2))
Returns 12:00am (0:00 hours) on August 30, 2013, or 41516. (Note that the function strips the time portion of the date.)
WORKDAY.INTL() (Excel 2010 and newer)
For newer versions of Excel (2010 and later), there is another version of WORKDAY called WORKDAY.INTL. WORKDAY.INTL works just like WORKDAY, but it adds the ability to customize the definition of the “weekend”. The syntax for WORKDAY.INTL is as follows:
=WORKDAY.INTL(start_date, days, [weekend], [holidays])
The start_date, days, and [holidays] inputs work just like the normal WORKDAY function.
The [weekend] input has the following options:
Retrieving Dates in Excel
DAY(), MONTH(), and YEAR()
Now we know how define dates, but we still need to be able to work with them. Serial numbers don’t make it easy to extract months, years, and days, nevermind hours, minutes, and seconds. That’s why Excel has specific functions for pulling out each of these values. For working with the calendar, there is DAY, MONTH, and YEAR. The syntax is simple:
=DAY(serial_number)
=MONTH(serial_number)
=YEAR(serial_number)
The serial_number in each can be any date-formatted cell reference. For example, if cell A1 stores September 10, 2013, we can use each of the formulas in turn:
=DAY(A1)
Returns 10 as a numeric value.
=MONTH(A1)
Returns 9 as a numeric value.
=YEAR(A1)
Returns 2013 as a numeric value.
We could have also given the direct serial number for September 10, 2013:
=DAY(41527)
Returns 10 as a numeric value.
Retrieving Times in Excel
HOUR(), MINUTE(), and SECOND()
For times, the process is very similar. Excel has function to retrieve the hours, minutes, and seconds from a time stamp, conveniently named HOUR, MINUTE, and SECOND. The syntax is identical:
=HOUR(serial_number)
=MINUTE(serial_number)
=SECOND(serial_number)
The serial_number in each can be any time/date-formatted cell reference. For example, if A1 stores 6:15:30pm (18:15 hours, 30 seconds) on September 10, 2013, we can use each of the formulas in turn:
=HOUR(A1)
Returns 18 as a numeric value.
=MINUTE(A1)
Returns 15 as a numeric value.
=SECOND(A1)
Returns 30 as a numeric value.
We could have also given the direct serial number for 6:15:30pm (18:15 hours, 30 seconds) on September 10, 2013:
=SECOND(41527.7607638889)
Returns 30 as a numeric value.
Additional Date Retrieving Functions
WEEKDAY() and WEEKNUM()
Dates don’t just have month and year information. They also encode indirect information… September 10, 2013 happens to also be a Tuesday. Excel has a few of functions to work with the week aspect of dates: WEEKDAY and WEEKNUM. The syntax is as follows:
=WEEKDAY(serial_number, [return_type])
=WEEKNUM(serial_number, [return_type])
The serial_number in each can be any date-formatted cell reference. Since [return_type] is optional, each function assumes that each week starts on Sunday. If cell A1 stores September 10, 2013 (a Tuesday), we can use each of the formulas in turn:
=WEEKDAY(A1)
Returns 3, since Tuesday is the 3rd day of a week that starts on Sunday.
=WEEKNUM(A1)
Returns 37, since September 10, 2013 is in the 37th week of 2013, when you start counting weeks from Sunday.
The [return_type] allows you to specify a different default week arrangement. You could let the week start on Monday and run until Sunday, or Saturday until Friday, for example. Excel is annoying, however, and makes the entry different for WEEKDAY and WEEKNUM. The full list of options for WEEKDAY is as follows:
Options 2 and 11 are functionally the same – the first is just there for backwards compatibility with earlier versions of Excel.
The full list of options for WEEKNUM is as follows:
Counting and Tracking Dates
Dates can be added and subtracted like normal numbers because they’re stored as serial numbers. That lets you count the days between two different dates. Sometimes, though, you need to count by a different metric.
NETWORKDAYS()
Above, we learned about WORKDAY, which lets you move back and forth a set number of workdays, ignoring weekends and holidays. But what if you need to measure the number of workdays between two dates? For that, Excel provides NETWORKDAYS. The formula syntax is as follows:
=NETWORKDAYS(start_date, end_date, [holidays])
The start_date and end_date can be any date-formatted cell reference.
The [holidays] input is optional, but lets you disqualify specific days (like Thanksgiving or Christmas, for example), which might otherwise fall during the work week. These are date serial numbers provided in an array bounded by brackets: { }. To specify multiple holidays, the dates must be held in cells – it is not possible to put multiple DATE functions in an array.
For example, if A1 contains 6:00pm (18:00 hours) on September 10, 2013 and B1 contains 9:00am (9:00 hours) on December 2, 2013, we can use NETWORKDAYS to find the number of workdays between the two dates.
Let’s exclude Columbus Day (October 14, 2013), Veterans Day (November 11, 2013), and Thanksgiving Day (November 28, 2013) as holidays. To do so, we have to store those dates in other cells. Let’s put them in C1, C2, and C3:
=DATE(2013,10,14)
=DATE(2013,11,11)
=DATE(2013,11,28)
Now, we can combine them in the function:
=NETWORKDAYS(A1,B1,C1:C3)
The function returns 57 as a numeric value.
NETWORKDAYS.INTL() (Excel 2010 and newer)
For newer versions of Excel (2010 and later), there is another version of NETWORKDAYS called NETWORKDAYS.INTL. NETWORKDAYS.INTL works just like NETWORKDAYS, but it adds the ability to customize the definition of the “weekend”. The syntax for NETWORKDAYS.INTL is as follows:
=NETWORKDAYS.INTL(start_date, end_date, [weekend], [holidays])
The start_date, end_date, and [holidays] inputs work just like the normal WORKDAY function.
The [weekend] input has the following options:
YEARFRAC()
Sometimes it’s useful to measure how much time has passed in years, but subtracting the YEAR function will only round down to the nearest full year. YEARFRAC takes two dates and provides the portion of the year between them. The syntax is as follows:
=YEARFRAC(start_date, end_date, [basis])
The start_date and end_date can be any date-formatted cell reference.
The [basis] input is optional, but lets you specify the “rules” for measuring the difference. Most of the time, you’ll want to use option 1, but here is the full list:
For example, if A1 contains September 10, 2013 and B1 contains December 2, 2013, we can use YEARFRAC to find the decimal portion of a year between the two dates:
=YEARFRAC(A1,B1,1)
Returns 0.227397260273973.
DATEDIF() (Undocumented Function)
The YEARFRAC function gives you the difference between dates as a fraction of a year, but sometimes you need more control. There is a powerful hidden function in Excel called DATEDIF that can do much more. It can tell you the number of years, months, or days between two dates. It can also track based on only partial inputs, ignoring years or months when calculating days. The syntax for DATEDIF is as follows:
=DATEDIF(start_date, end_date, unit)
The start_date and end_date can be any date-formatted cell reference.
The unit input asks you to specify a string that represents the type of output you want. This is slightly cumbersome, since you must wrap the input in quotes (” “).
For example, if A1 contains September 10, 2012 and B1 contains December 2, 2013, we can use DATEDIF to find the number of full years between the two dates:
=DATEDIF(A1,B1,"Y")
Returns 1 as a numeric value.
Using the same start_date and end_date inputs, here are the output possibilities for DATEDIF using different unit parameters:
Every time, the unit must be put in quotes (e.g. “Y” or “MD”).
Converting Dates and Times from Text
DATEVALUE() and TIMEVALUE()
All of the above functions work perfectly with date-formatted serial numbers in Excel. Unfortunately, dates and times are often imported into worksheets as text. Most of the assorted functions like MONTH and HOUR are reasonably intelligent about converting on the fly. Occasionally it’s useful to build a date value through concatenation. The two functions Excel provides for this purpose are DATEVALUE and TIMEVALUE. The syntax for each is as follows:
=DATEVALUE(date_text)
=TIMEVALUE(time_text)
The date_text and time_text accept any text string that looks like a date or time.
This is how DATEVALUE responds to various date_text inputs:
This is how TIMEVALUE responds to various time_text inputs:
Converting Dates and Times to Text
TEXT()
Getting data converted to dates and times is great, but you may also need to get it back out. Sometimes, you need a special format. Other times, you need to look for a date in a text string, and have to match using string tools like FIND and SEARCH. There is one master function for converting dates and times to text strings in Excel, called TEXT. The syntax for TEXT is as follows:
=TEXT(value, format_text)
The value can be any date or time-formatted cell reference.
The format_text input has a large number of options, summarized here:
The outputs can be combined with simple formatting characters to produce standard date formats. Using the date 5:07:03am (05:07 hours, 3 seconds) on September 10, 2013, here are examples of possible outputs:
A Common Problem
One issue people frequently run into is that Excel occasionally misinterprets text fields as dates. An example is here:
Be careful when entering dates, especially if you are importing from other data sources, to make sure that your “Jan-13” is being stored as January 1, 2013 and not January 13, 2013!
Get the latest Excel tips and tricks by joining the newsletter!
Andrew Roberts has been solving business problems with Microsoft Excel for over a decade. Excel Tactics is dedicated to helping you master it.
Join the newsletter to stay on top of the latest articles. Sign up and you’ll get a free guide with 10 time-saving keyboard shortcuts!
Other posts in this series…
Insert the current date and time in a cell
Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web Excel 2021 Excel 2021 for Mac Excel 2019 Excel 2019 for Mac Excel 2016 Excel 2016 for Mac Excel 2013 Excel 2010 Excel 2007 Excel for Mac 2011 More…Less
Let’s say that you want to easily enter the current date and time while making a time log of activities. Or perhaps you want to display the current date and time automatically in a cell every time formulas are recalculated. There are several ways to insert the current date and time in a cell.
Insert a static date or time into an Excel cell
A static value in a worksheet is one that doesn’t change when the worksheet is recalculated or opened. When you press a key combination such as Ctrl+; to insert the current date in a cell, Excel “takes a snapshot” of the current date and then inserts the date in the cell. Because that cell’s value doesn’t change, it’s considered static.
-
On a worksheet, select the cell into which you want to insert the current date or time.
-
Do one of the following:
-
To insert the current date, press Ctrl+; (semi-colon).
-
To insert the current time, press Ctrl+Shift+; (semi-colon).
-
To insert the current date and time, press Ctrl+; (semi-colon), then press Space, and then press Ctrl+Shift+; (semi-colon).
-
Change the date or time format
To change the date or time format, right-click on a cell, and select Format Cells. Then, on the Format Cells dialog box, in the Number tab, under Category, click Date or Time and in the Type list, select a type, and click OK.
Insert a static date or time into an Excel cell
A static value in a worksheet is one that doesn’t change when the worksheet is recalculated or opened. When you press a key combination such as Ctrl+; to insert the current date in a cell, Excel “takes a snapshot” of the current date and then inserts the date in the cell. Because that cell’s value doesn’t change, it’s considered static.
-
On a worksheet, select the cell into which you want to insert the current date or time.
-
Do one of the following:
-
To insert the current date, press Ctrl+; (semi-colon).
-
To insert the current time, press + ; (semi-colon).
-
To insert the current date and time, press Ctrl+; (semi-colon), then press Space, and then press + ; (semi-colon).
-
Change the date or time format
To change the date or time format, right-click on a cell, and select Format Cells. Then, on the Format Cells dialog box, in the Number tab, under Category, click Date or Time and in the Type list, select a type, and click OK.
Insert a static date or time into an Excel cell
A static value in a worksheet is one that doesn’t change when the worksheet is recalculated or opened. When you press a key combination such as Ctrl+; to insert the current date in a cell, Excel “takes a snapshot” of the current date and then inserts the date in the cell. Because that cell’s value doesn’t change, it’s considered static.
-
On a worksheet, select the cell into which you want to insert the current date or time.
-
Do one of the following:
-
To insert the date, type the date (like 2/2), and then click Home > Number Format dropdown (in the Number tab) >Short Date or Long Date.
-
To insert the time, type the time, and then click Home > Number Format dropdown (in the Number tab) >Time.
-
Change the date or time format
To change the date or time format, right-click on a cell, and select Number Format. Then, on the Number Format dialog box, under Category, click Date or Time and in the Type list, select a type, and click OK.
Insert a date or time whose value is updated
A date or time that updates when the worksheet is recalculated or the workbook is opened is considered “dynamic” instead of static. In a worksheet, the most common way to return a dynamic date or time in a cell is by using a worksheet function.
To insert the current date or time so that it is updatable, use the TODAY and NOW functions, as shown in the following example. For more information about how to use these functions, see TODAY function and NOW function.
For example:
Formula |
Description (Result) |
=TODAY() |
Current date (varies) |
=NOW() |
Current date and time (varies) |
-
Select the text in the table shown above, and then press Ctrl+C.
-
In the blank worksheet, click once in cell A1, and then press Ctrl+V. If you are working in Excel for the web, repeat copying and pasting for each cell in the example.
Important: For the example to work properly, you must paste it into cell A1 of the worksheet.
-
To switch between viewing the results and viewing the formulas that return the results, press Ctrl+` (grave accent), or on the Formulas tab, in the Formula Auditing group, click the Show Formulas button.
After you copy the example to a blank worksheet, you can adapt it to suit your needs.
Note: The results of the TODAY and NOW functions change only when the worksheet is calculated or when a macro that contains the function is run. Cells that contain these functions are not updated continuously. The date and time that are used are taken from the computer’s system clock.
Need more help?
You can always ask an expert in the Excel Tech Community or get support in the Answers community.
Need more help?
Want more options?
Explore subscription benefits, browse training courses, learn how to secure your device, and more.
Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.
The objective of this post is to teach you how Excel handles date and time and provide you with all the tools you will need.
It’s designed to be read in conjunction with the accompanying Excel file, which you can download below.
Download the Files
Enter your email address below to download the comprehensive Excel workbook and PDF.
By submitting your email address you agree that we can email you our Excel newsletter.
Regional Settings
When reading this post keep in mind that my regional settings format dates as dd/mm/yyyy and so the screenshots throughout this post are in this format. However, if you open the accompanying Excel file you may see some dates have switched to match your regional settings, which may be different to mine e.g. mm/dd/yyyy.
Dates and times with a format that begins with an asterisk (*) automatically update based on your PC’s regional settings. You can see an example in the Format Cells dialog box below:
Ok, let’s crack on.
Excel Date and Time 101
Excel stores dates and time as a number known as the date serial number, or date-time serial number.
When you look at a date in Excel it’s actually a regular number that has been formatted to look like a date. If you change the cell format to ‘General’ you’ll see the underlying date serial number.
The integer portion of the date serial number represents the day, and the decimal portion is the time. Dates start from 1st January 1900 i.e. 1/1/1900 has a date serial number of 1.
Caution! Excel dates after 28th February 1900 are actually one day out. Excel behaves as though the date 29th February 1900 existed, which it didn’t.
Microsoft intentionally included this bug in Excel so that it would remain compatible with the spreadsheet program that had the majority market share at the time; Lotus 1-2-3.
Lotus 1-2-3 was incorrectly programmed as though 1900 was a leap year. This isn’t a problem as long as all your dates are later than 1st March 1900.
Excel gives each date a numeric value starting at 1st January 1900. 1st January 1900 has a numeric value of 1, the 2nd January 1900 has a numeric value of 2 and so on. These are called ‘date serial numbers’, and they enable us to do math calculations and use dates in formulas.
The Date Serial Number column displays the Date column values in their date serial number equivalent.
e.g. 1/1/2017 has a date serial number of 42736. i.e. 1st January 2017 is 42,736 days since 31st December 1899.
Tip: format the date serial number column as a Date and you’ll see they look the same as the Date column values.
Time
Times also use a serial number format and are represented as decimal fractions.
Hours: since 24 hours = 1 day, we can infer that 24 hours has a time serial number of 1, which can be formatted as time to display 24:00 or 12:00 AM or 0:00. Whereas 12 hours or the time 12:00 has a value of 0.50 because it is half of 24 hours or half of a day, and 1 hour is 0.41666′ because it’s 1/24 of a day.
Minutes: since 1 hour is 1/24 of a day, and 1 minute is 1/60 of an hour, we can also say that 1 minute is 1/1440 of a day, or its time serial number is 0.00069444′
Seconds: since a second is 1/60 of a minute, which is 1/60 of an hour, which is 1/24 of a day. We can also say one second is 1/86400 of a day or in time serial number form it’s 0.0000115740740740741…
Date & Time Together
Now that we know how dates and times are stored we can put them together — ddddd.tttttt
For example, the date and time of 1st January 2012 10:00:00 AM has a date-time serial value of 40909.4166666667
40909 being the serial value representing the date 1st January 2012, and .4166666667 being the decimal value for the time 10:00 AM and 00 seconds.
More examples below.
Entering Dates & Times in Excel
Entering Dates
You can type in various configurations of a date and Excel will automatically recognise it as a date and upon pressing ENTER it will convert it to a date serial number and apply a date format on the cell.
For example, try typing (or even copy and paste) the following dates into an empty cell:
1-1-2009 |
1-1-09 |
1/1/2009 |
1/1/09 |
1-Jan-09 |
1-Jan 09 |
1-Jan-2009 |
1 Jan 09 |
1/1 |
You can see in the table above that entering numbers that look like dates and are separated by a forward slash or hyphen will be recognised as a date. Even typing in a date with the month name gets converted to a date.
However, dates separated with a period like this 1.1.2009, or with spaces between numbers like this 01 01 2009, will end up as text, not a date. Gotta have some limits!
Tip: Dates that display ##### in a cell usually indicate that the column is simply not wide enough to display it.
However, if you make the cell really wide and it still displays ##### then this indicates that the date is a negative value and Excel can’t display negative dates.
Entering Dates with Two Digit Years
When you enter a date with two digits for the year e.g. 1/1/09, Excel has to decide if you mean 2009 or 1909.
It goes by the rule that dates with years 29 or before, are treated as 20xx and dates with the year 30 or older are treated as 19xx. See examples below.
Tip: You can enter the day and month portions of a date and Excel will insert the year based on your computer’s clock. Nice to know for data entry.
Entering Time
When you enter time you must follow a strict format of at least h:mm. i.e. the hour and minutes are separated by a colon with no spaces either side. Entering the h:mm components will result in a time formatted in military time e.g. 2:00 PM is 14:00 in military time.
If you enter a time that includes a seconds component e.g. 3:15:40, Excel will automatically format the cell in h:mm:ss.
If you want the time to be formatted with AM/PM you can simply enter a space after the time and then type AM or PM, or apply the number format to the cell later. Here are some examples:
Entering Dates & Time Together
Now that we know how to enter dates and time separately we can put them together to enter a date and time in the same cell.
You can even enter time then date and Excel will fix the order for you.
You’ll find that even if you enter AM/PM, that Excel will convert it to military time by default. You can override this with a custom number format. More on that later.
Simple Date & Time Math
Now that we understand that Excel stores dates and time as serial numbers, you’ll see how logical it is to perform math operations on these values. We’ll look at some simple examples here and tackle the more complex scenarios later when we look at Date and Time Functions.
Adding/Subtracting Days from Dates
Tip: you can also add/subtract the days directly in the formula e.g. =B10+10 or =B11-5 Although, it’s better to place the values you’re adjusting by in their own cell or a named range.
Subtracting Dates from one another
Tip: format the cell to General or Number to see the number of days between two dates.
Note: the ‘result’ is exclusive of the start day i.e. it assumes the start day is at the end of that day.
Adding Times to one another
The time being added is input as a time serial number. Notice there are no negative times in the table below. Remember we can’t display negative times. Instead we need to use the math operator to tell Excel to subtract time. See examples below.
Note: Times that roll over to the next day result in a time-date serial number >= 1. Cell E28 actually contains a time-serial number of 1.08333′, but since the cell is formatted to display time formatted as h:mm:ss, only the time portion is visible.
If you want to show the cumulative time (like cell E29) then you need to surround the ‘h’ part of the time format in square brackets like so: [h]:mm:ss
Subtracting Time from Times
Notice the last result in the table below shows ######, this is because it results in a negative time and Excel can’t display that, but notice it can return a negative time serial number. More on how to solve this later.
Subtracting Times from one another
Again, here the last result shows ###### because it results in a negative time.
Excel Date and Time Shortcuts
‘Good to Know’ Stuff about Excel Date and Time
— Dates prior to 1st January 1900 are not recognised in Excel.
— A negative date will display in the cell as #######
— Times stored without a date effectively inherit the date 0 Jan 1900 i.e. the month is Jan and the year 1900 and the day is zero. Remember, there are no dates prior to 1/1/1900 from Excel’s perspective. This means that times stored without a date e.g. 0.50 for 12:00 PM is the equivalent of 0 Jan 1900 12:00 PM.
This is important because if you try to take 14 hours from 12 hours (without a date) you’ll get the dreaded ###### display in the cell, because negative dates and times cannot be displayed. We’ll cover workarounds for this later, but for now keep in mind that math on dates and time that result in negative date-time serial numbers cannot be formatted as a date.
Date Modes
— Excel actually has two date modes. The other mode is called 1904 Date System and is used for compatibility with Excel 2008 for Mac and earlier Mac versions. You can change the date system in the Advanced Options.
In the 1904 date system dates are calculated using 1st January 1904 as the starting point. The difference between the two date systems is 1,462 days. This means that the serial number of a date in the 1900 date system is always 1,462 days greater than the serial number of the same date in the 1904 date system. 1,462 days is equal to four years and one day (including one leap day).
Caution; the date setting you choose applies to all dates within the workbook. You can’t mix and match modes and you shouldn’t reference workbooks that use a different date system in formulas.
Bottom line; don’t use the 1904 date system unless absolutely necessary! Click here for more on date systems in Excel.
— Excel applies date number formats based on your system region settings. For example, my system is set to display dates in dd/mm/yyyy format, but if you’re in the U.S. your system is likely to format them as mm/dd/yyyy. Excel will automatically convert the format of date serial numbers to suit your system settings as long as it’s one of the default date formats and not a custom number format.
More Excel Date and Time Tips
This post is just the beginning, the next steps in mastering Excel Date and Time are below:
- Every Excel Date and Time Function explained
- Formatting Date and Time in Excel
- Common Date and Time Calculations
Tip: Avoid waiting, download the workbook and get the above topics now.
Enter your email address below to download the comprehensive Excel workbook and PDF.
By submitting your email address you agree that we can email you our Excel newsletter.
Since dates and times are stored as numbers in the back end in Excel, you can easily use simple arithmetic operations and formulas on the date and time values.
For example, you can add two different time values or date values or you can calculate the time difference between two given dates/times.
In this tutorial, I will show you a couple of ways to perform calculations using time in Excel (such as calculating the time difference, adding or subtracting time, showing time in different formats, and doing a sum of time values)
How Excel Handles Date and Time?
As I mentioned, dates and times are stored as numbers in a cell in Excel. A whole number represents a complete day and the decimal part of a number would represent the part of the day (which can be converted into hours, minutes, and seconds values)
For example, the value 1 represents 01 Jan 1900 in Excel, which is the starting point from which Excel starts considering the dates.
So, 2 would mean 02 Jan 1990, 3 would mean 03 Jan 1900 and so on, and 44197 would mean 01 Jan 2021.
Note: Excel for Windows and Excel for Mac follow different starting dates. 1 in Excel for Windows would mean 1 Jan 1900 and 1 in Excel for Mac would mean 1 Jan 1904
If there are any digits after a decimal point in these numbers, Excel would consider those as part of the day and it can be converted into hours, minutes, and seconds.
For example, 44197.5 would mean 01 Jan 2021 12:00:00 PM.
So if you’re working with time values in Excel, you would essentially be working with the decimal portion of a number.
And Excel gives you the flexibility to convert that decimal portion into different formats such as hours only, minutes only, seconds only, or a combination of hours, minutes, and seconds
Now that you understand how time is stored in Excel, let’s have a look at some examples of how to calculate the time difference between two different dates or times in Excel
Formulas to Calculating Time Difference Between Two Times
In many cases, all you want to do is find out the total time that has elapsed between the two-time values (such as in the case of a timesheet that has the In-time and the Out-time).
The method you choose would depend on how the time is mentioned in a cell and in what format you want the result.
Let’s have a look at a couple of examples
Simple Subtraction of Calculate Time Difference in Excel
Since time is stored as a number in Excel, find the difference between 2 time values, you can easily subtract the start time from the end time.
End Time – Start Time
The result of the subtraction would also be a decimal value that would represent the time that has elapsed between the two time-values.
Below is an example where I have the start and the end time and I have calculated the time difference with a simple subtraction.
There is a possibility that your results are shown in the time format (instead of decimals or in hours/minutes values). In our above example, the result in cell C2 shows 09:30 AM instead of 9.5.
That’s perfectly fine as Excel tries to copy the format from the adjacent column.
To convert this into a decimal, change the format of the cells to General (the option is in the Home tab in the Numbers group)
Once you have the result, you can format it in different ways. For example, you can show the value in hours only or minutes only or a combination of hours, minutes, and seconds.
Below are the different formats you can use:
Format | What it Does |
h | Shows only the hours elapsed between the two dates |
hh | Shows hours in double-digit (such as 04 or 12) |
hh:mm | Shows hours and minutes elapsed between the two dates, such as 10:20 |
hh:mm:ss | Shows hours, minutes, and seconds elapsed between the two dates, such as 10:20:36 |
And if you’re wondering where and how to apply these custom date formats, follow the below steps:
- Select the cells where you want to apply the date format
- Hold the Control key and press the 1 key (or Command + 1 if using Mac)
- In the Format Cells dialog box that opens, click on the Number tab (if not selected already)
- In the left pane, click on Custom
- Enter any of the desired format code in the Type field (in this example, I am using hh:mm:ss)
- Click OK
The above steps would change the formatting and show you the value based on the format.
Note that custom number formatting does not change the value in the cell. It only changes the way a value is being displayed. So, I can choose to only show the hour value in a cell, while it would still have the original value.
Pro tip: If the total number of hours exceeds 24 hours, use the following custom number format instead: [hh]:mm:ss
Calculate the Time Difference in Hours, Minutes, or Seconds
When you subtract the time values, Excel returns a decimal number that represents the resulting time difference.
Since every whole number represents one day, the decimal part of the number would represent that much part of the day which can easily be converted into hours or minutes, or seconds.
Calculating Time Difference in Hours
Suppose you have the dataset as shown below and you want to calculate the number of hours between the two-time values
Below is the formula that will give you the time difference in hours:
=(B2-A2)*24
The above formula will give you the total number of hours elapsed between the two-time values.
Sometimes, Excel tries to be helpful and will give you the result in time format as well (as shown below).
You can easily convert this into Number format by clicking on the Home tab, and in the Number group, selecting Number as the format.
If you only want to return the total number of hours elapsed between the two times (without any decimal part), use the below formula:
=INT((B2-A2)*24)
Note: This formula only works when both the time values are of the same day. If the day changes (where one of the time values is of another date and the second one of another date), This formula will give wrong results. Have a look at the section where I cover the formula to calculate the time difference when the date changes later in this tutorial.
Calculating Time Difference in Minutes
To calculate the time difference in minutes, you need to multiply the resulting value by the total number of minutes in a day (which is 1440 or 24*60).
Suppose you have a data set as shown below and you want to calculate the total number of minutes elapsed between the start and the end date.
Below is the formula that will do that:
=(B2-A2)*24*60
Calculating Time Difference in Seconds
To calculate the time difference in seconds, you need to multiply the resulting value by the total number of seconds in a day (which is or 24*60*60 or 86400).
Suppose you have a data set as shown below and you want to calculate the total number of seconds that have elapsed between the start and end date.
Below is the formula that will do that:
=(B2-A2)*24*60*60
Calculating time difference with the TEXT function
Another easy way to quickly get the time difference without worrying about changing the format is to use the TEXT function.
The TEXT function allows you to specify the format right within the formula.
=TEXT(End Date - Start Date, Format)
The first argument is the calculation you want to do, and the second argument is the format in which you want to show the result of the calculation.
Suppose you have a dataset as shown below and you want to calculate the time difference between the two times.
Here are some formulas that will give you the result with different formats
Show only the number of hours:
=TEXT(B2-A2,"hh")
The above formula will only give you the result that shows the numbers of hours elapsed between the two-time values. If your result is 9 hours and 30 minutes, it will still show you 9 only.
Show the number of total minutes
=TEXT(B2-A2,"[mm]")
Show the number of total seconds
=TEXT(B2-A2,"[ss]")
Show Hours and Minutes
=TEXT(B2-A2,"[hh]:mm")
Show Hours, Minutes, and Seconds
=TEXT(B2-A2,"hh:mm:ss")
If you’re wondering what’s the difference between hh and [hh] in the format (or mm and [mm]), when you use the square brackets, it would give you the total number of hours between the two dates, even if the hour value is higher than 24. So if you subtract two date values where the difference is more than24 hours, using [hh] will give you the total number of hours and hh will only give you the hours elapsed on the day of the end date.
Get the Time Difference in One-Unit (Hours/Minutes) and Ignore Others
If you want to calculate the time difference between the two time-values in only the number of hours or minutes or seconds, then you can use the dedicated HOUR, MINUTE, or SECOND function.
Each of these functions takes one single argument, which is the time value, and returns the specified time unit.
Suppose you have a data set as shown below, and you want to calculate the total number of hours minutes, and seconds that have elapsed between these two times.
Below are the formulas to do this:
Calculating Hours Elapsed Between two times
=HOUR(B2-A2)
Calculating Minutes from the time value result (excluding the completed hours)
=MINUTE(B2-A2)
Calculating Seconds from the time value result (excluding the completed hours and minutes)
=SECOND(B2-A2)
A couple of things that you need to know when working with these HOURS, MINUTE, and SECOND formulas:
- The difference between the end time and the start time cannot be negative (which is often the case when the date changes). In such cases, these formulas would return a #NUM! error
- These formulas only use the time portion of the resulting time value (and ignore the day portion). So if the difference in the end time and the start time is 2 Days, 10 Hours, 32 Minutes, and 44 Seconds, the HOUR formula will give 10, the MINUTE formula will give 32, and the SECOND formula will give 44
Calculate elapsed time Till Now (from the start time)
If you want to calculate the total time that has elapsed between the start time and the current time, you can use the NOW formula instead of the End time.
NOW function returns the current date and the time in the cell in which it is used. It’s one of those functions that does not take any input argument.
So, if you want to calculate the total time that has elapsed between the start time and the current time, you can use the below formula:
=NOW() - Start Time
Below is an example where I have the start times in column A, and the time elapsed till now in column B.
If the difference in time between the start date and time and the current time is more than 24 hours, then you can format the result to show the day as well as the time portion.
You can do that by using the below TEXT formula:
=TEXT(NOW()-A2,"dd hh:ss:mm")
You can also achieve the same thing by changing the custom formatting of the cell (as covered earlier in this tutorial) so that it shows the day as well as the time portion.
In case your start time only has the time portion, then Excel would consider it as the time on 1st January 1990.
In this case, if you use the NOW function to calculate the time elapsed till now, it is going to give you the wrong result (as the resulting value would also have the total days that have elapsed since 1st Jan 1990).
In such a case, you can use the below formula:
=NOW()- INT(NOW())-A2
The above formula uses the INT function to remove the day portion from the value returned by the now function, and this is then used to calculate the time difference.
Note that NOW is a volatile function that updates whenever there is a change in the worksheet, but it does not update in real-time
Calculate Time When Date Changes (calculate and display negative times in Excel)
The methods covered so far work well if your end time is later than the start time.
But the problem arises when your end time is lower than the start time. This often happens when you are filling timesheets where you only enter the time and not the entire date and time.
In such cases, if you’re working in a one night shift and the date changes, there is a possibility that your end time would be earlier than your start time.
For example, if you start your work at 6:00 PM in the evening, and complete your work & time out at 9:00 AM in the morning.
If you only working with time values, then subtracting the start time from the end time is going to give you a negative value of 9 hours (9 – 18).
And Excel cannot handle negative time values (and for that matter nor can humans, unless you can time travel)
In such cases, you need a way to figure out that the day has changed and the calculation should be done accordingly.
Thankfully, there is a really easy fix for this.
Suppose you have a dataset as shown below, where I have the start time and the end time.
As you would notice that sometimes the start time is in the evening and the end time is in the morning (which indicates that this was an overnight shift and the day has changed).
If I use the below formula to calculate the time difference, it will show me the hash signs in the cells where the result is a negative value (highlighted in yellow in the below image).
=B2-A2
Here is an IF formula that whether the time difference value is negative or not, and in case it is negative then it returns the right result
=IF((B2-A2)<0,1-(A2-B2),(B2-A2))
While this works well in most cases, it still falls short in case the start time and the end time is more than 24 hours apart. For example, someone signs in at 9:00 AM on day 1 and sign-out at 11:00 AM on day 2.
Since this is more than 24 hours, there is no way to know whether the person has signed out after 2 hours or after 26 hours.
While the best way to tackle this would be to make sure that the entries include the date as well as the time, but if it’s just the time that you’re working with, then the above formula should take care of most of the issues (considering it’s unlikely for anyone to work for more than 24 hours)
Adding/ Subtracting Time in Excel
So far, we have seen examples where we had the start and the end time, and we needed to find the time difference.
Excel also allows you to easily add or subtract a fixed time value from the existing date and time value.
For example, let’s say you have a list of queued tasks where each task takes the specified time, and you want to know when each task will end.
In such a case, you can easily add the time that each task is going to take to the start time to know at what time is the task expected to be completed.
Since Excel stores date and time values as numbers, you have to ensure that the time you are trying to add abides by the format that Excel already follows.
For example, if you add 1 to a date in Excel, it is going to give you the next date. This is because 1 represents an entire day in Excel (which is equal to 24 hours).
So if you want to add 1 hour to an existing time value, you cannot go ahead and simply add 1 to it. you have to make sure that you convert that hour value into the decimal portion that represents one hour. and the same goes for adding minutes and seconds.
Using the TIME Function
Time function in Excel takes the hour value, the minute value, and the seconds value and converts it into a decimal number that represents this time.
For example, if I want to add 4 hours to an existing time, I can use the below formula:
=Start Time + TIME(4,0,0)
This is useful if you know how many hours, minutes, and seconds you want to add to an existing time and simply use the TIME function without worrying about the correct conversion of the time into a decimal value.
Also, note that the TIME function will only consider the integer part of the hour, minute, and seconds value that you input. For example, if I use 5.5 hours in the TIME function it would only add five hours and ignore the decimal part.
Also, note that the TIME function can only add values that are less than 24 hours. If your hour value is more than 24, this would give you an incorrect result.
And the same goes with the minutes and the second’s part where the function will only consider values that are less than 60 minutes and 60 seconds
Just like I have added time using the TIME function, you can also subtract time. Just change the + sign to a negative sign in the above formulas
Using Basic Arithmetic
When the time function is easy and convenient to use, it does come with a few restrictions (as covered above).
If you want more control, you can use the arithmetic method that I’ll cover here.
The concept is simple – convert the time value into a decimal value that represents the portion of the day, and then you can add it to any time value in Excel.
For example, if you want to add 24 hours to an existing time value, you can use the below formula:
=Start_time + 24/24
This just means that I’m adding one day to the existing time value.
Now taking the same concept forward let’s say you want to add 30 hours to a time value, you can use the below formula:
=Start_time + 30/24
The above formula does the same thing, where the integer part of the (30/24) would represent the total number of days in the time that you want to add, and the decimal part would represent the hours/minutes/seconds
Similarly, if you have a specific number of minutes that you want to add to a time value then you can use the below formula:
=Start_time + (Minutes to Add)/24*60
And if you have the number of seconds that you want to add, then you can use the below formula:
=Start_time + (Minutes to Add)/24*60*60
While this method is not as easy as using the time function, I find it a lot better because it works in all situations and follows the same concept. unlike the time function, you don’t have to worry about whether the time that you want to add is less than 24 hours or more than 24 hours
You can follow the same concept while subtracting time as well. Just change the + to a negative sign in the formulas above
How to SUM time in Excel
Sometimes you may want to quickly add up all the time values in Excel. Adding multiple time values in Excel is quite straightforward (all it takes is a simple SUM formula)
But there are a few things you need to know when you add time in Excel, specifically the cell format that is going to show you the result.
Let’s have a look at an example.
Below I have a list of tasks along with the time each task will take in column B, and I want to quickly add these times and know the total time it is going to take for all these tasks.
In cell B9, I have used a simple SUM formula to calculate the total time all these tasks are going to take, and it gives me the value as 18:30 (which means that it is going to take 18 hours and 20 minutes to complete all these tasks)
All good so far!
How to SUM over 24 hours in Excel
Now see what happens when I change at the time it is going to take Task 2 to complete from 1 hour to 10 hours.
The result now says 03:20, which means that it should take 3 hours and 20 minutes to complete all these tasks.
This is incorrect (obviously)
The problem here is not Excel messing up. The problem here is that the cell is formatted in such a way that it will only show you the time portion of the resulting value.
And since the resulting value here is more than 24 hours, Excel decided to convert the 24 hours part into a day remove it from the value that is shown to the user, and only display the remaining hours, minutes, and seconds.
This, thankfully, has an easy fix.
All you need to do is change the cell format to force it to show hours even if it exceeds 24 hours.
Below are some formats that you can use:
Format | Expected Result |
[h]:mm | 28:30 |
[m]:ss | 1710:00 |
d “D” hh:mm | 1 D 04:30 |
d “D” hh “Min” ss “Sec” | 1 D 04 Min 00 Sec |
d “Day” hh “Minute” ss “Seconds” | 1 Day 04 Minute 00 Seconds |
You can change the format by going to the format cells dialog box and applying the custom format, or use the TEXT function and use any of the above formats in the formula itself
You can use the below the TEXT formula to show the time, even when it’s more than 24 hours:
=TEXT(SUM(B2:B7),"[h]:mm:ss")
or the below formula if you want to convert the hours exceeding 24 hours to days:
=TEXT(SUM(B2:B7),"dd hh:mm:ss")
Results Showing Hash (###) Instead of Date/Time (Reasons + Fix)
In some cases, you may find that instead of showing you the time value, Excel is displaying the hash symbols in the cell.
Here are some possible reasons and the ways to fix these:
The column is not wide enough
When a cell doesn’t have enough space to show the complete date, it may show the hash symbols.
It has an easy fix – change the column width and make it wider.
Negative Date Value
A date or time value cannot be negative in Excel. in case you are calculating the time difference and it turns out to be negative, Excel will show you hash symbols.
The way to fixes to change the formula to give you the right result. For example, if you are calculating the time difference between the two times, and the date changes, you need to adjust the formula to account for it.
In other cases, you may use the ABS function to convert the negative time value into a positive number so that it’s displayed correctly. Alternatively, you can also an IF formula to check if the result is a negative value and return something more meaningful.
In this tutorial, I covered topics about calculating time in Excel (where you can calculate the time difference, add or subtract time, show time in different formats, and sum time values)
I hope you found this tutorial useful.
Other Excel tutorials you may also like:
- Convert Time to Decimal Number in Excel (Hours, Minutes, Seconds)
- How to Remove Time from Date/Timestamp in Excel
- How to Calculate the Number of Days Between Two Dates in Excel
- Combine Date and Time in Excel
- How to Calculate Years of Service in Excel
- How to Convert Seconds to Minutes in Excel