Python Friday #134: Date and Time in Python (Part 1: Date)

I waited a long time to address date and time because that topic has the potential to get complicated quickly. In a side project I needed some date arithmetic and that is a good opportunity to look at date and time in Python.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

The datetime module

Python offers these main objects around date and time in as part of the datetime module:

  • Date: an idealised Gregorian calendar that assumes it always existed in the past and will always exist in the future.
  • Time: an idealised Time in which a day is always 24*60*60 seconds long – no “leap seconds” or any other specialities.
  • Datetime: the combination of date and time in one object.
  • Timedelta: the duration between two date, time, or datetime instances.
  • Timezone: a fixed offset from UTC to represent time zone information.

With those objects we can represent date and time in a way that is easy to work with. However, since it is an idealised representation of date and time, it will not work if your calculations depend on all the specialities in time and date like leap seconds or other calendars like the Julian calendar.

 

Creating a date object

I find these 3 ways to create a date object the most useful:

The today() method is a special case to get current date, while the other two approaches allow us to create any date you want.

 

Inspecting the date object

When we get our date instance, we can ask it for various parts of a date:

The difference between the two methods to find the day of the week is with what value Monday starts:

  • weekday(): Monday is 0 and Sunday is 6.
  • isoweekday(): Monday is 1 and Sunday is 7

 

Get the delta between two dates

If we want to know the number of days between two dates, we can subtract one date from the other. This gives us a timedelta object with the answer:

The timedelta object stores the difference as seconds and converts it for us to days:

 

Date arithmetic

If we want to add a day to our date object, we can express the difference in a timedelta object and use the + operator to add them:

The same works for subtraction. We need to convert the difference we want into a timedelta object and then use the operator:

 

Calendars

When you want to know the week of a year a day is in, we can create a calendar to answer this question. We start with the date object for the day we are interested in and then call the isocalendar() method:

The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday in that specific year. Therefore, an ISO year consists of 52 or 53 full weeks.

With the inverse method fromisocalendar() we can create a date based on the day of a week in a year. To get the Tuesday of week 50 in 2022, we can write this code:

 

Next

With these basics we can work with dates in Python. Next week we take a closer look at datetime, where we can work with a more granular unit of time to express a point in time.

2 thoughts on “Python Friday #134: Date and Time in Python (Part 1: Date)”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.