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.
Directive | Example | Description |
---|---|---|
%a | Wed | Weekday, short version, usually three characters in length |
%A | Wednesday | Weekday, full version |
%w | 3 | Weekday as a number 0-6, 0 is Sunday |
%d | 31 | Day of month 01-31 |
%b | Dec | Month name, short version, usually three characters in length |
%B | December | Month name, full version |
%m | 12 | Month as a number 01-12, January is 01 |
%y | 21 | Year, short version, without century (2021) |
%Y | 2021 | Year, full version |
%H | 17 | Hour 00-23 (24 hour format) |
%I | 05 | Hour 00-12 (12 hour format) |
%p | PM | AM/PM |
%M | 35 | Minute 00-59 |
%S | 14 | Second 00-59 |
%f | 638745 | Microsecond 000000-999999 |
%z | +0530 | UTC offset |
%Z | CST | Timezone |
%j | 182 | Day number of year 001-366 (366 for leap year, 365 otherwise) |
%U | 47 | Week number of year, Sunday as the first day of week, 00-53 |
%W | 51 | Week number of year, Monday as the first day of week, 00-53 |
%c | Tue Dec 10 17:41:00 2019 | Local version of date and time |
%x | 12/10/19 | Local version of date (mm/dd/yy) |
%X | 17:41:00 | Local 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.