Python Date Time String Format
Discussion of usage of strfttime and strptime functions in python to produce and consume Date time strings
6 min read
Date and time are two of the most useful yet confusing things in programming life. Date time storage and retrieval required a common well-decided protocol. Timezone conversion is also one of the most challenging tasks. That's why while working with Date and Time, developers should be extra curious. When it comes to working with date and time, Python's
datetime library is the most reliable one. The Datetime library tries to solve all the problems to a large extent. So that's why developers love to use it.
While creating a user-oriented application, one of the most common tasks is to create a well-formatted Date-time string. Example:
Friday, 7 October 2022.
Another useful task is to convert the Date-time string into a
datetime object so that we can use that object with the rest of the program.
Python's Datetime Object is an object which stores information on a particular date and time. It stores
timezone information as parameters. The object has various methods to do almost everything possible.
The scope of this blog is the formatting of the date-time object, but not the description of the object itself. So if you want to know more, I am adding some references.
Conversion of user-given String to Python's Datetime Object
Often time, when users give a date or time input, they often use the specified format by the app. Some common formats are
HH:MM:SS, like so. Now, it's the responsibility of the developer to convert the given string to a more useful object.
To convert user given string to a datetime object we use
from datetime import datetime datetime.strptime(date_string, format_string) -> Datetime
date_string is the input from the user, and
format_string is the expected format, in which the user has given the
format_string has the format specifiers, which will be used to extract the values from the
from datetime import datetime datetime.strptime("07/10/2022", "%d/%m/%Y") # datetime.datetime(2022, 10, 7, 0, 0)
The date_string (
07/10/2022) is mapped with the format string (
|Date string value||Format string value||Explanation|
|/||Ignored, as not a format specifier|
|/||Ignored, as not a format specifier|
See the Format list below.
Conversion Datetime Object to user-readable string
Now that, you have a new
datetime object and you have done some operations with that, you need to properly format it to string so that users can understand it very well. So now our task is to convert the object to a string.
To convert a datetime object to a string, we use
# Method 1 datetime_object.strftime(format_string) -> formatted_string # Method 2 from datetime import datetime datetime.strftime(Datetime, format_string) -> formatted_string
Method 1, we use
strftimeas a method call to the datetime object and pass the format_string as a parameter.
Method 2, we are directly calling the
strftimeas a function and passing the datetime object as the first param, and format_string as the second param.
For both cases, it'll return a
formatted date and time string.
from datetime import datetime datetime.now().strftime("%d/%m/%Y") # '07/10/2022' (current date in the above format)
Format Specifiers List
Below there are all format specifiers used while formatting. The format specifies are grouped by their types.
|Abbreviated weekday name.||Sun, Mon, ...|
|Full weekday name.||Sunday, Monday, ...|
|Weekday as a decimal number.||0, 1, ..., 6|
|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, 01, ..., 53|
|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, 01, ..., 53|
|Day of the month as a zero-padded decimal.||01, 02, ..., 31|
|Day of the month as a decimal number.||1, 2, ..., 31|
|Day of the year as a zero-padded decimal number.||001, 002, ..., 366|
|Day of the year as a decimal number.||1, 2, ..., 366|
|Abbreviated month name.||Jan, Feb, ..., Dec|
|Full month name.||January, February, ...|
|Month as a zero-padded decimal number.||01, 02, ..., 12|
|Month as a decimal number.||1, 2, ..., 12|
|Year without century as a zero-padded decimal number.||00, 01, ..., 99|
|Year without century as a decimal number.||0, 1, ..., 99|
|Year with century as a decimal number.||2013, 2019 etc.|
|Hour (24-hour clock) as a zero-padded decimal number.||00, 01, ..., 23|
|Hour (24-hour clock) as a decimal number.||0, 1, ..., 23|
|Hour (12-hour clock) as a zero-padded decimal number.||01, 02, ..., 12|
|Hour (12-hour clock) as a decimal number.||1, 2, ... 12|
|Locale’s AM or PM.||AM, PM|
|Minute as a zero-padded decimal number.||00, 01, ..., 59|
|Minute as a decimal number.||0, 1, ..., 59|
|Second as a zero-padded decimal number.||00, 01, ..., 59|
|Second as a decimal number.||0, 1, ..., 59|
|Microsecond as a decimal number, zero-padded on the left.||000000 - 999999|
|UTC offset in the form +HHMM or -HHMM.|
|Time zone name.|
|Locale’s appropriate date and time representation.||Mon Sep 30 07:06:05 2013|
|Locale’s appropriate date representation.||09/30/13|
|Locale’s appropriate time representation.||07:06:05|
|A literal '%' character.||%|
Did you find this article valuable?
Support Arkadip Bhattacharya by becoming a sponsor. Any amount is appreciated!