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:
Code | Description | Output |
---|---|---|
%a | Abbreviated weekday name. | Sun, Mon, … |
%A | Weekday as full name. | Sunday, Monday, … |
%w | Weekday as a decimal number. | 0, 1, 3, …, 6 |
%d | Day of the month with a zero appended. | 01, 02, …, 31 |
%-d | Day of the month as a decimal number. | 1, 2, …, 30 |
%b | Abbreviated month name. | Jan, Feb, …, Dec |
%B | Full month name. | January, February, … |
%m | Month decimal number with a zero appended. | 01, 02, …, 12 |
%-m | Month as a decimal number. | 1, 2, …, 12 |
%y | Year without century in decimal number with a zero appended. | 00, 01, …, 99 |
%-y | Year without century. | 0, 1, …, 99 |
%Y | Year with century. | 2015, 2021 etc. |
%H | 24 Hours clock from 00 to 23. | 00, 01, …, 23 |
%-H | 24 Hours clock from 0 to 23. | 0, 1, …, 23 |
%I | 12 Hour clock from 01 to 12. | 01, 02, …, 12 |
%-I | 12 Hour clock from 01 to 12. | 1, 2, … 12 |
%p | Locale’s AM or PM. | AM, PM |
%M | Minute in decimal number from 00 to 59. | 00, 01, …, 59 |
%-M | Minute as a decimal number. | 0, 1, …, 59 |
%S | Second in decimal number from 00 to 59 | 00, 01, …, 59 |
%-S | Second as a decimal number. | 0, 1, …, 59 |
%f | Microsecond as a decimal number with a zero appended on the left. | 000000 – 999999 |
%z | UTC offset in the form +HHMM or -HHMM. | |
%Z | Time zone name. | |
%j | The day of the year as a decimal number with a zero appended. | 001, 002, …, 366 |
%-j | The Day of the year as a decimal number. | 1, 2, …, 366 |
%U | The year’s week number (Sunday as the first day of the week). | 00, 01, …, 53 |
%W | Week 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