Lesson ยท 16 min
Dates and times
A date in a file is text
"2026-06-01T08:15" is text. You cannot subtract one text from another text, and a text does not know its weekday. Python has a type for a moment in time. Its name is datetime, and it is in the module that has the same name.
from datetime import datetime
moment = datetime.fromisoformat("2026-06-01T08:15")
print(moment.year, moment.month, moment.day) # 2026 6 1
print(moment.hour, moment.minute) # 8 15
The form 2026-06-01T08:15 is the ISO format: the year, the month, the day, the letter T, and the time. fromisoformat reads it. Most programs can write this format. Use it in your own files. One more advantage: when you sort ISO dates as text, they are also in the order of time.
The weekday
print(moment.weekday()) # 0
print(moment.strftime("%A")) # Monday
weekday() gives a number. Monday is 0, and Sunday is 6. A number of 5 or more is a day of the weekend.
strftime makes text from a moment. The codes that start with % say what you want.
| Code | Meaning | Example |
|---|---|---|
%A |
the name of the weekday | Monday |
%d |
the day of the month | 01 |
%m |
the month as a number | 06 |
%B |
the name of the month | June |
%Y |
the year | 2026 |
%H:%M |
the hour and the minute | 08:15 |
moment.strftime("%d %B %Y") gives 01 June 2026.
The time between two moments
Subtract one moment from the other moment. The result is a length of time. Its method total_seconds() gives the seconds.
start = datetime.fromisoformat("2026-06-01T08:15")
end = datetime.fromisoformat("2026-06-01T09:00")
length = end - start
print(length.total_seconds() / 60) # 45.0
This also works across midnight, and across the end of a month. Python knows the calendar.
Dates in other forms
Many files do not use the ISO format. strptime reads a text with the codes of the table above. You give it the text, and then the form of the text.
moment = datetime.strptime("01/06/2026 08:15", "%d/%m/%Y %H:%M")
Be careful with a date such as 01/06/2026. In some countries it is 1 June, and in other countries it is 6 January. The file does not tell you which one it is. Ask the person who made the file, or look for a day number above 12 in the column.
Your turn
main.py loads 14 trips. Each trip has the moment when it started, and the moment when it ended. All the values are text. Do the tasks in order.
Tasks
5 to do
Task 1, not checked yet. Write a function
minutes_between(start_text, end_text). Each argument is a moment in the ISO format, such as"2026-06-01T08:15". The function returns the whole minutes from the first moment to the second moment.Task 2, not checked yet. Write a function
day_name(text). It takes a moment in the ISO format and returns the name of the weekday, such as"Monday".Task 3, not checked yet. Add two keys to each trip in
trips.minutesis the length of the trip in whole minutes.dayis the name of the weekday on which the trip starts. Use your two functions.Task 4, not checked yet. Make a variable
weekend_tripswith the number of trips that start on a Saturday or a Sunday.Task 5, not checked yet. Make a variable
busiest_hourwith the hour of the day in which the most trips start. It is a whole number from0to23.
Task 1 of 5
Write a function minutes_between(start_text, end_text). Each argument is a moment in the ISO format, such as "2026-06-01T08:15". The function returns the whole minutes from the first moment to the second moment.