Loading...
Loading...
Test date format strings with live preview
%YYear with century20244 digits%yYear without century242 digits%mMonth as number0301-12%BFull month nameMarch%bAbbreviated monthMar%dDay of month1501-31%jDay of year075001-366%AFull weekdayFriday%aAbbreviated weekdayFri%wWeekday number50=Sun%HHour (24-hour)1400-23%IHour (12-hour)0201-12%MMinute3000-59%SSecond4500-59%fMicrosecond0000006 digits%pAM or PMPM%zUTC offset+0530%ZTimezone nameIST%%Literal %%strftime (string format time) converts a datetime object into a formatted string, while strptime (string parse time) does the reverse - it parses a date string and converts it into a datetime object. Think of strftime as "datetime to string" and strptime as "string to datetime."
%Y is the four-digit year, %m is the zero-padded month, %d is the zero-padded day, %H is the 24-hour clock hour, %M is the minute, and %S is the second. Together, this format produces output like "2026-04-03 14:30:00" and is one of the most common datetime formats in Python.
Use the format string "%B %d, %Y" with strftime. For example: datetime.now().strftime("%B %d, %Y"). The %B code gives the full month name, %d gives the zero-padded day, and %Y gives the four-digit year.
strptime raises a ValueError when the input string does not match the format string you provided. Common causes include mismatched separators (using slashes when the format expects dashes), wrong date components (like month and day swapped), or extra whitespace in the input string. Double-check that your format codes exactly match the structure of your date string.
Use %I for the 12-hour clock hour and %p for AM or PM. For example: datetime.now().strftime("%I:%M %p") produces output like "02:30 PM". Note that %I is zero-padded, so 9 AM displays as "09:00 AM."