Engineering #14: .isocalendar!
Short easy one today.
In an exercise the ISO week date system came up. I had only heard of it. It’s used in business (fiscal years, production schedules, etc). Weeks start on Monday. The first week of the year always contains the first Thursday of the year. I asked ChatGPT why Thursday:
thursday is the mid-point of the week in iso: weeks start monday, so thursday is the 4th day.
it guarantees that week 1 is the first week that’s mostly in the new year (at least 4 days).
The datetime module in Python has a class called date, which contains ways to access and provide dates. Date has an isocalendar method, which will return a tuple with: the ISO year, ISO week number, and ISO weekday.
The easiest way to access this in Python is:
from datetime import date
# we have to manually import the date class into our namespace, to use things it contains, like methods .today and .isocalendar
>>> date.today() # the regular, non-ISO info
datetime.date(2025, 6, 28)
>>> date.today().isocalendar() # ISO info
datetime.IsoCalendarDate(year=2025, week=26, weekday=6)
I’m writing this on Saturday, so: 6th day of the week per ISO.