Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Time
Date
7. Calculating differences with timedeltas
Datetime
Summary

Instruction

Perfect! You can also calculate the time difference between two dates. Let's try it with trip dates. How long did the following trip last?

fra_mad_scheduled = datetime.date(2018, 12, 1)
mad_fra_scheduled = datetime.date(2018, 12, 7)
trip_duration = mad_fra_scheduled - fra_mad_scheduled
print('My trip lasted for', trip_duration)

Result:

My trip lasted for 6 days, 0:00:00.

By subtracting one date from another (mad_fra_scheduled - fra_mad_scheduled), we created a timedelta in the trip_duration variable. timedeltas are objects that store time duration. In addition to doing other things, we can print them to the console. We'll talk more about timedeltas later in this part.

Exercise

How much time elapsed between Daniel Higgins' first day with our company and the company's anniversary? Use the anniversary and higgins_start_day variables to find out. Print the following line:

Daniel Higgins has worked at our company for {timedelta}

Stuck? Here's a hint!

To calculate the timedelta, simply use:

anniversary - higgins_start_day