datetime Module

July 30, 2025 · Python Module , TIL

The datetime module is handy when dealing with dates, times, and durations in Python. The four core classes are:

Common patterns to remember

A sample of the format codes for formating the datetime objects:

CodeMeaningExample
%Y4-digit year2025
%y2-digit year25
%mMonth (01-12)07
%BFull month nameJuly
%bShort month nameJul
%dDay (01-31)30
%HHour 24-hour (00-23)14
%IHour 12-hour (01-12)02
%MMinute (00-59)30
%SSecond (00-59)45
%pAM/PMPM

Gotchas to be mindful of