Python DateTime Format - Examples


Python DateTime Format

You can format a date string using datetime Python package.

datetime package provides directives to access a specific part of date, time or datetime object of datetime package.

First, we will present you all the directives (or wildcard characters) that could be used to format a date and time string. Then we will proceed with examples, on how to use these directive to construct a required format for date.

Directives

Following is table of directives with an example and description for each of them.

DirectiveExampleDescription
%aWedWeekday, short version, usually three characters in length
%AWednesdayWeekday, full version
%w3Weekday as a number 0-6, 0 is Sunday
%d31Day of month 01-31
%bDecMonth name, short version, usually three characters in length
%BDecemberMonth name, full version
%m12Month as a number 01-12, January is 01
%y 21 Year, short version, without century (2021)
%Y2021Year, full version
%H17Hour 00-23 (24 hour format)
%I05Hour 00-12 (12 hour format)
%pPMAM/PM
%M35Minute 00-59
%S14Second 00-59
%f638745Microsecond 000000-999999
%z+0530UTC offset
%ZCSTTimezone
%j182Day number of year 001-366 (366 for leap year, 365 otherwise)
%U47Week number of year, Sunday as the first day of week, 00-53
%W51Week number of year, Monday as the first day of week, 00-53
%cTue Dec 10 17:41:00 2019Local version of date and time
%x12/10/19Local version of date (mm/dd/yy)
%X17:41:00Local version of time (hh:mm:ss)
%%%A % character

Examples

1. Format current datetime

In this example, we will get the current time and extract different parts of the date. With these we will format different kinds of date strings.

Python Program

from datetime import datetime

dt = datetime.now()

print(dt)
print('\nDirectives\n--------------')
print(dt.strftime('Weekday short version : %a'))
print(dt.strftime('Weekday full version  : %A'))
print(dt.strftime('Weekday as a number   : %w'))
print(dt.strftime('Day of month          : %d'))
print(dt.strftime('Month Name short ver  : %d'))
print(dt.strftime('Month Name full ver   : %b'))
print(dt.strftime('Month as a number     : %m'))
print(dt.strftime('Year short version    : %y'))
print(dt.strftime('Year full version     : %Y'))
print(dt.strftime('Hour (00-23)          : %H'))
print(dt.strftime('Hour (00-11)          : %I'))
print(dt.strftime('AM/PM                 : %p'))
print(dt.strftime('Minute                : %M'))
print(dt.strftime('Second                : %S'))


print('\nFormatted Date Strings\n--------------')
print(dt.strftime('%a %d-%m-%Y'))
print(dt.strftime('%a %d/%m/%Y'))
print(dt.strftime('%a %d/%m/%y'))
print(dt.strftime('%A %d-%m-%Y, %H:%M:%S'))
print(dt.strftime('%X %x'))

Output

2019-12-10 14:43:35.542195

Directives
--------------
Weekday short version : Tue
Weekday full version  : Tuesday
Weekday as a number   : 2
Day of month          : 10
Month Name short ver  : 10
Month Name full ver   : Dec
Month as a number     : 12
Year short version    : 19
Year full version     : 2019
Hour (00-23)          : 14
Hour (00-11)          : 02
AM/PM                 : PM
Minute                : 43
Second                : 35

Formatted Date Strings
--------------
Tue 10-12-2019
Tue 10/12/2019
Tue 10/12/19
Tuesday 10-12-2019, 14:43:35
14:43:35 12/10/19

Summary

In this tutorial of Python Examples, we learned how to format datetime in Python, with the help of well detailed examples.