How to Format Date and Datetime in Python

Sovary May 28, 2022 466
3 minutes read

We are human have date time system the same, but some countries use date format differently. This article will go to detail on Python how to format date in different format. I will explain step by step and give planty examples using date format, so you will learn along this short tutorial.

strftime() - return string as date format following custom input.

Syntax

strftime(format): string

Return Value: string

Parameters

Parameters Type Description
format String (Required) String to input format code date time

The below is format code to use with the method strftime()

 

 

Format Code

Code Description Example
%a Abbreviated weekday name. Sun,...,Sat
%A Full weekday name. Sunday,.., Saturday
%w Weekday as a decimal number. 0,..,6
%d Day of the month as a zero-padded decimal. 01,..,31
%-d Day of the month as a decimal number. 1, 2,..,31
%b Abbreviated month name. Jan,..,Dec
%B Full month name. January,..,December
%m Month as a zero-padded decimal number. 01,..,12
%-m Month as a decimal number. 1,..,12
%y Year without century as a zero-padded decimal number. 00,..,99
%-y Year without century as a decimal number. 0,..,99
%Y Year with century as a decimal number. 2022, 2023,..
%H Hour (24-hour clock) as a zero-padded decimal number. 00,..,23
%-H Hour (24-hour clock) as a decimal number. 0,..,23
%I Hour (12-hour clock) as a zero-padded decimal number. 01,..,12
%-I Hour (12-hour clock) as a decimal number. 1,..,12
%p Locale’s AM or PM. AM, PM
%M Minute as a zero-padded decimal number. 00,..,59
%-M Minute as a decimal number. 0, 1, ..., 59
%S Second as a zero-padded decimal number. 00, 01, ..., 59
%-S Second as a decimal number. 0, 1, ..., 59
%f Microsecond as a decimal number, zero-padded on the left. 000000 - 999999
%z UTC offset in the form +HHMM or -HHMM. +0000
%Z Time zone name. UTC
%j Day of the year as a zero-padded decimal number. 001,..,366
%-j Day of the year as a decimal number. 1,..,366
%U Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. 00,..,53
%W Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. 00,..,53
%c Locale’s appropriate date and time representation. Mon Sep 30 11:36:45 2022
%x Locale’s appropriate date representation. 12/31/22
%X Locale’s appropriate time representation. 11:36:45
%% A literal '%' character. %

Examples

Example #1 - Using strftime() convert datetime object to different date format

from datetime import datetime

now = datetime.now()
# format show dd/mm/yy
format1 = now.strftime("%d/%m/%Y")
print("format1: ", format1)

# format time: 15:19:34
time = now.strftime("%H:%M:%S")
print("time:", time)

# format 05/28/2022 - 03:19:34 PM
date_time = now.strftime("%d-%b-%Y - %I:%M:%S %p")
print("date and time:",date_time)

Output:

format1:  28/05/2022
time: 15:24:07
date and time: 28-May-2022 - 03:24:07 PM

 

 

 Example #2 - Using strftime() convert date object to different date format

from datetime import date

# get only date time is nothing
today = date.today()
# format show dd/mm/yy
format1 = today.strftime("%d/%m/%Y")
print("format1: ", format1)

# format time: 15:19:34
time = today.strftime("%H:%M:%S")
print("time:", time)

# format 05/28/2022 - 03:19:34 PM
date_time = today.strftime("%d-%b-%Y - %I:%M:%S %p")
print("date and time:",date_time)

 Output:

format1:  28/05/2022
time: 00:00:00
date and time: 28-May-2022 - 12:00:00 AM

 You can see the time is zero, because date object provide only date info, the time is reset default to 12am.

Hope you learn something with this short article. Have a nice day!

You might Also Like:

Python 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him