Sling Academy
Home/Python/How to Format Date and Time in Python

How to Format Date and Time in Python

Last updated: January 04, 2023

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.

Next Article: Python: 2 Ways to Check if a Date String is Valid

Previous Article: Python: Calculate person’s age based on ISO 8601 date string birthday

Series: Date and Time in Python

Python

You May Also Like

  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types
  • Python: Typing a function with *args and **kwargs