Modules and the standard library

INPUT · Slides

Work out a date

01 / 07

What date is thirty days from now?

Working out "thirty days from today" yourself turns out to be surprisingly hard. Months have thirty days, or thirty-one, in no obvious order, and February has twenty-eight — twenty-nine in a leap year.

Hardly anybody can write all of that correctly on their own. So you hand it to the datetime module.

Getting the date today takes one line.

from datetime import dateprint(date.today())

That one, though, gives a different answer depending on the day you run it. So that the marking can work, this lesson makes its own dates and uses those.

02 / 07

Make a date with date

date(year, month, day) makes a single date. Shown on screen it looks like 2026-04-01.

From a date you have made, .year, .month and .day get the pieces back out. As with math.pi, these are values, so no brackets.

from datetime import dateday = date(2026, 4, 1)print(day)print(day.year, day.month, day.day)

Result

2026-04-01
2026 4 1

03 / 07

Add and subtract with timedelta

timedelta stands for a length of time. timedelta(days=30) is thirty days of it.

You can add one to a date or take one away. The good part is that it gets it right across a month boundary, and in a leap year.

from datetime import date, timedeltaday = date(2026, 4, 1)print(day + timedelta(days=30))print(day - timedelta(days=1))

Result

2026-05-01
2026-03-31

04 / 07

Subtract one date from another

Take a date away from a date and you get the stretch in between. Shown as it is, that looks like 34 days, 0:00:00.

When you only want the number of days, add .days. That is a "how many days to go" program, right there.

from datetime import datestart = date(2026, 4, 1)end = date(2026, 5, 5)gap = end - startprint(gap)print(gap.days)

Result

34 days, 0:00:00
34

05 / 07

datetime carries the time too

date is the date and nothing else. When you want the time of day as well, use datetime, in the form datetime(year, month, day, hour, minute).

There is a tool inside the module with the same name as the module, which is how you end up writing from datetime import datetime. It is confusing, and it is simply how it is written — take it as given.

from datetime import datetime, timedeltamoment = datetime(2026, 4, 1, 9, 30)print(moment)print(moment + timedelta(hours=3))

Result

2026-04-01 09:30:00
2026-04-01 12:30:00

06 / 07

Set out the look with strftime

You will not always want 2026-04-01; sometimes you want 01/04/2026. That is what strftime is for.

  • %Y … the year in four digits
  • %m … the month in two digits
  • %d … the day in two digits

There is no need to learn the codes. Know that such a thing exists, and look it up when you need it — that is enough.

from datetime import dateday = date(2026, 4, 1)print(day.strftime("%Y/%m/%d"))print(day.strftime("%d/%m/%Y"))

Result

2026/04/01
01/04/2026

07 / 07

Do not write the calendar yourself

Working with dates has far more traps in it than it looks. Deciding whether a year is a leap year alone has exceptions every four years, every hundred and every four hundred.

Writing that out yourself is practice at getting it wrong. Whatever you want to do, somebody has probably already made it. When a date turns up, think of datetime first.