Let's get started! To use date and/or time data in Python, we have to import the datetime
module first, like this:
import datetime
Now, let's define our first time variable. We'll be dealing with flight connections, and we want to define the scheduled time of departure for a flight from Frankfurt Airport (FRA) to Madrid Airport (MAD):
fra_mad_scheduled = datetime.time(9, 30, 0)
print(fra_mad_scheduled)
Result: 09:30:00
By invoking datetime.time(9, 30, 0)
, we created a new time object with the following arguments: hour=9, minute=30, second=0
. Our fra_mad_scheduled
variable now points to exactly 9:30 AM, but it is not associated with any specific date or time zone.
Remember that all time and date objects from the datetime
module use the 24-hour clock. To show a 9:30 PM departure, use:
fra_mad_scheduled = datetime.time(21, 30, 0)