How to Format Date and Time in Python

Updated: January 4, 2023 By: Frienzied Flame Post a comment

In Python, you can call the strftime() method on a datetime object to create a human-friendly, readable string. This method takes a string parameter that specifies how the date and time should be formatted.

Syntax:

my_datetime_object.strftime("format string")

Python provides a wide range of character codes that can be used with the strftime() method. These character codes are used to specify the different components of the date and time values. The most popular ones include %b for the first three characters of the month name, %d for the day of the month, %Y for the year in four-digit format, %H for the hour, %M for the minute, and %S for the second. You can see the full list of these things at the end of this article.

The format string is very flexible, and you can modify it it to get a customized result for each country, each language, desired length, and precision (only date or include hours, minutes, second).

Examples

Here is a minimal example:

# Import the datetime module
import datetime

# Get the current date and time
now = datetime.datetime.now()

# Format the date and time into a readable string
readable_date = now.strftime("%m/%d/%Y, %H:%M:%S")
print("The current date and time is: " + readable_date)

Output:

The current date and time is: 01/04/2023, 17:42:20

It is important to note that the result of the code snippet above depends on when you execute it.

Another example:

import datetime 

date = datetime.datetime(2024, 1, 1, 10, 36, 48, 9999)

formatted_date_1 = date.strftime("Japanese way to format date: %Y年%m月%d日 %H時%M分%S秒") 
print(formatted_date_1)

formatted_date_2 = date.strftime("Lazy way to format date: %c")
print(formatted_date_2)

formatted_date_3 = date.strftime("Something will happen at %H:%M:%S, on %d, %B %Y")
print(formatted_date_3)

formatted_date_4 = date.strftime("US way to format date %m/%d/%Y %H:%M:%S")
print(formatted_date_4)

Output:

Japanese way to format date: 2024年01月01日 10時36分48秒
Lazy way to format date: Mon Jan  1 10:36:48 2024
Something will happen at 10:36:48, on 01, January 2024
US way to format date 01/01/2024 10:36:48

Valid Date Format Codes in Python

Here is the full list of legal format codes for date and time in Python, along with detailed descriptions:

  • %a: Returns the first three characters of the weekday, e.g., Mon, Tue.
  • %A: Returns the full name of the weekday, e.g., Sunday, Monday.
  • %w: Returns the weekday as a decimal number, where 0 is Sunday, and 6 is Saturday.
  • %d: Returns the day of the month as a zero-padded decimal number (01 to 31).
  • %-d: Returns the day of the month as a decimal number. (Platform specific)
  • %b: Returns the month as the locale’s abbreviated name.
  • %B: Returns the month as the locale’s full name.
  • %m: Returns the month as a zero-padded decimal number.
  • %-m: Returns the month as a decimal number. (Platform specific)
  • %y: Returns the year without century as a zero-padded decimal number.
  • %Y: Returns the year with century as a decimal number.
  • %H: Returns the hour (24-hour clock) as a zero-padded decimal number.
  • %-H: Returns the hour (24-hour clock) as a decimal number. (Platform specific)
  • %I: Returns the hour (12-hour clock) as a zero-padded decimal number.
  • %-I: Returns the hour (12-hour clock) as a decimal number. (Platform specific)
  • %p: Returns the locale’s equivalent of either AM or PM.
  • %M: Returns the minute as a zero-padded decimal number.
  • %-M: Returns the minute as a decimal number. (Platform specific)
  • %S: Returns the second as a zero-padded decimal number.
  • %-S: Returns the second as a decimal number. (Platform specific)
  • %f: Returns the microsecond as a decimal number, zero-padded on the left.
  • %z: Returns the UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).
  • %Z: Returns the UTC time zone name (empty string if the object is naive).
  • %j: Returns the day of the year as a zero-padded decimal number.
  • %-j: Returns the day of the year as a decimal number. (Platform specific)
  • %U: Returns the week number of the year (Sunday as the first day of the week) as a zero-padded integer (00 to 53). All days in a new year preceding the first Sunday are considered to be in week 0.
  • %W: Returns the week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.
  • %c: Returns the locale’s appropriate date and time representation.
  • %x: Returns the locale’s appropriate date representation.
  • %X: Returns the locale’s appropriate time representation.
  • %%: Returns a literal % character.

If you have questions regarding formatting dates in Python, please leave a comment. We are happy to answer them.