18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Working with Dates and Times 13<br />

Calculating the difference between two times<br />

Because times are represented as serial numbers, you can subtract the earlier time from the later time to get<br />

the difference. For example, if cell A2 contains 5:30:00 and cell B2 contains 14:00:00, the following formula<br />

returns 08:30:00 (a difference of eight hours and 30 minutes):<br />

=B2-A2<br />

If the subtraction results in a negative value, however, it becomes an invalid time; Excel displays a series of<br />

hash marks (#######) because a time without a date has a date serial number of 0. A negative time results<br />

in a negative serial number, which is not permitted.<br />

If the direction of the time difference doesn’t matter, you can use the ABS function to return the absolute<br />

value of the difference:<br />

=ABS(B2-A2)<br />

This “negative time” problem often occurs when calculating an elapsed time — for example, calculating the<br />

number of hours worked given a start time and an end time. This presents no problem if the two times fall<br />

in the same day. But if the work shift spans midnight, the result is an invalid negative time. For example,<br />

you may start work at 10:00 PM and end work at 6:00 AM the next day. Figure 13.6 shows a worksheet<br />

that calculates the hours worked. As you can see, the shift that spans midnight presents a problem (cell C3).<br />

FIGURE 13.6<br />

Calculating the number of hours worked returns an error if the shift spans midnight.<br />

Using the ABS function (to calculate the absolute value) isn’t an option in this case because it returns the<br />

wrong result (16 hours). The following formula, however, does work:<br />

TIP<br />

=IF(B2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!