Skip to main content
Strftime() and Strptime() In Python

How To Use Strftime and Strptime In Python

The strftime() and strptime() methods from the Python datetime module will be covered in this post. The strptime() function turns a DateTime object into a string in the exact opposite way as the strftime() function.

You can get more information from the official docs Datetime Module.

What is the difference between strptime and strftime?

strptime converts the string to a datetime object, whereas strftime creates a formatted string for given datetime object according to the specified format by the user

Python Strftime Format with Example

The strftime() function is used to convert date and time objects to a string date representation.

syntax of strftime() method

dateobject.strftime(format)

The format parameter is the user’s preferred date string format. It returns the string representation of the date or time object.

The table below shows the codes that are used to create the format:

CodeDescriptionOutput
%aAbbreviated weekday name.Sun, Mon, …
%AWeekday as full name.Sunday, Monday, …
%wWeekday as a decimal number.0, 1, 3, …, 6
%dDay of the month with a zero appended.01, 02, …, 31
%-dDay of the month as a decimal number.1, 2, …, 30
%bAbbreviated month name.Jan, Feb, …, Dec
%BFull month name.January, February, …
%mMonth decimal number with a zero appended.01, 02, …, 12
%-mMonth as a decimal number.1, 2, …, 12
%yYear without century in decimal number with a zero appended.00, 01, …, 99
%-yYear without century.0, 1, …, 99
%YYear with century.2015, 2021 etc.
%H24 Hours clock from 00 to 23.00, 01, …, 23
%-H24 Hours clock from 0 to 23.0, 1, …, 23
%I12 Hour clock from 01 to 12.01, 02, …, 12
%-I12 Hour clock from 01 to 12.1, 2, … 12
%pLocale’s AM or PM.AM, PM
%MMinute in decimal number from 00 to 59.00, 01, …, 59
%-MMinute as a decimal number.0, 1, …, 59
%SSecond in decimal number from 00 to 5900, 01, …, 59
%-SSecond as a decimal number.0, 1, …, 59
%fMicrosecond as a decimal number with a zero appended on the left.000000 – 999999
%zUTC offset in the form +HHMM or -HHMM. 
%ZTime zone name. 
%jThe day of the year as a decimal number with a zero appended.001, 002, …, 366
%-jThe Day of the year as a decimal number.1, 2, …, 366
%UThe year’s week number (Sunday as the first day of the week).00, 01, …, 53
%WWeek number of the year (Monday as the first day of the week).00, 01, …, 53

Let’s create a sample python code to convert datetime object into a string.

import datetime
from datetime import datetime
now = datetime.now()
print(now)
print(now.strftime("%Y-%m-%d %H:%M:%S"))
print(now.strftime("%A %-m %Y"))

Output:

2021-10-04 11:01:57.586848
2021-10-04 11:01:57
Monday 10 2021

Python Strptime Format with Example

The strptime() function is used to create a datetime object from a string.

syntax of strptime() method

datetime.strptime(date_string, format)

The strptime() method takes two arguments: date_string desire format that is converted to datetime, and other is the format code.

The format code:

  • %d – Represents the day of the month: 01, 02, …, 31
  • %B – Full Month’s name: January, February etc.
  • %Y – Year in four digits: 2018, 2019 etc.

The python script to convert a string into DateTime:

from datetime import datetime
created_at = "10/4/2021 04:49:32"
dt_object = datetime.strptime(created_at, "%d/%m/%Y %H:%M:%S")
print(dt_object)

Output:

2021-04-10 04:49:32

Leave a Reply

Your email address will not be published. Required fields are marked *