Python Friday #135: Date and Time in Python (Part 2: Datetime)

After a deeper look at date last week, we explore in this post the datetime and time objects of 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.

 

Creating a datetime object

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

The now() constructor is a lot more granular than date.today(), since it gives us the current time up to the microsecond (1/1,000,000 of a second).

The fromtimestamp() method allows us to use a UNIX timestamp that counts the seconds from 1.1.1970 to create a datetime instance. That is especially helpful when an API returns timestamps instead of ISO dates.

 

Inspecting the datetime object

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

We can get the different parts of time back as we did above with datetime. The date() method is a great help when we just want the date part of the datetime object, while the time() method gives us only the time part.

 

Creating a time object

We can create a time object like this with only an hour part or with hours, minutes, seconds and microseconds:

The method fromisoformat() is the inverse operation to the isoformat() method, in which we get back a string representing the ISO time:

The different parts of the time object work the same way as the examples above.

 

Turn dates into strings and back

The two methods strftime() (hint: f = format for output) and strptime() (hint: p = parse) convert between datetime objects and strings.

We can print the release date of this post in different formats:

This output works with all date, datetime and time objects. However, it does not make sense to ask a time object for its day in the week.

We can take a string and turn it into a datetime object like this:

The formats for those codes are from the 1989 C standard. You can get the full list for Python in the documentation.

I find these format codes the most useful:

Directive Meaning Example
%a Weekday as locale’s abbreviated nam “Sun, Mon, …, Sat (en_US)”
“So, Mo, …, Sa (de_D)”
%A Weekday as locale’s full nam “Sunday, Monday, …, Saturday (en_US)”
“Sonntag, Montag, …, Samstag (de_D)”
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. “0, 1, …, 6”
%d Day of the month as a zero-padded decimal number. “01, 02, …, 31”
%b Month as locale’s abbreviated nam “Jan, Feb, …, Dec (en_US)”
“Jan, Feb, …, Dez (de_D)”
%B Month as locale’s full nam “January, February, …, December (en_US)”
“Januar, Februar, …, Dezember (de_D)”
%m Month as a zero-padded decimal number. “01, 02, …, 12”
%y Year without century as a zero-padded decimal number. “00, 01, …, 99”
%Y Year with century as a decimal number. “0001, 0002, …, 2013, 2014, …, 9999”
%H Hour (24-hour clock) as a zero-padded decimal number. “00, 01, …, 23”
%M Minute as a zero-padded decimal number. “00, 01, …, 59”
%S Second as a zero-padded decimal number. “00, 01, …, 59”
%f Microsecond as a decimal number, zero-padded to 6 digits. “000000, 000001, …, 999999”
%j Day of the year as a zero-padded decimal number. “001, 002, …, 366”
%U Week number of the year (Sunday as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. “00, 01, …, 53”
%W Week number of the year (Monday as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first Monday are considered to be in week 0. “00, 01, …, 53”
%c Locale’s appropriate date and time representatio Tue Aug 16 21:30:00 1988 (en_US)
Di 16 Aug 21:30:00 1988 (de_DE)
%x Locale’s appropriate date representatio 08/16/88 (None)
08/16/1988 (en_US);
16.08.1988 (de_DE)
%X Locale’s appropriate time representatio 21:30:00 (en_US)
21:30:00 (de_DE)
%% A literal ‘%’ characte %

 

Next

With datetime and time we can represent a point in time that is more granular than a date. Next week we look at the dateutil package and how it helps us to create relative dates and work with time zones.

1 thought on “Python Friday #135: Date and Time in Python (Part 2: Datetime)”

Leave a Comment

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