Converting Datetime to String via strftime

๐Ÿท๏ธ Working with Dates and Time / Formatting and Parsing Dates

When working with dates and times in Python, you'll often need to convert a datetime object into a human-readable string. This is especially useful when generating log entries, creating timestamps for filenames, or formatting dates for reports. The strftime method (string format time) gives you full control over how the date and time appear.


โš™๏ธ What is strftime?

strftime is a method available on datetime objects that converts them into a formatted string. You provide a format string containing special codes (called directives), and Python replaces each code with the corresponding date or time component.

  • The method is called directly on a datetime object.
  • The format string uses % followed by a letter to represent different date/time parts.
  • The result is always a string that you can print, save, or use in other operations.

๐Ÿ“Š Common Format Directives

Here are the most frequently used format codes for building your date strings:

Directive Meaning Example Output
%Y Year with century (4 digits) 2025
%y Year without century (2 digits) 25
%m Month as zero-padded number 01 to 12
%B Full month name January
%b Abbreviated month name Jan
%d Day of month (zero-padded) 01 to 31
%A Full weekday name Monday
%a Abbreviated weekday name Mon
%H Hour (24-hour clock) 00 to 23
%I Hour (12-hour clock) 01 to 12
%M Minute (zero-padded) 00 to 59
%S Second (zero-padded) 00 to 59
%p AM or PM AM
%f Microsecond (6 digits) 000000 to 999999

๐Ÿ› ๏ธ Basic Usage Examples

Example 1: Simple date string - Create a datetime object: from datetime import datetime then now = datetime.now() - Convert to string: date_string = now.strftime("%Y-%m-%d") - Result: 2025-04-07

Example 2: Full readable format - Format string: "%B %d, %Y" - Result: April 07, 2025

Example 3: Including time - Format string: "%Y-%m-%d %H:%M:%S" - Result: 2025-04-07 14:30:15

Example 4: 12-hour clock with AM/PM - Format string: "%I:%M %p on %A, %B %d" - Result: 02:30 PM on Monday, April 07


๐Ÿ•ต๏ธ Common Patterns for Engineers

Here are some practical format patterns you'll likely use in your daily work:

For log timestamps: - Pattern: "%Y-%m-%d %H:%M:%S" - Output: 2025-04-07 14:30:15

For filenames (no spaces or colons): - Pattern: "%Y%m%d_%H%M%S" - Output: 20250407_143015

For human-readable logs: - Pattern: "%a %b %d %H:%M:%S %Y" - Output: Mon Apr 07 14:30:15 2025

For ISO 8601 format: - Pattern: "%Y-%m-%dT%H:%M:%S" - Output: 2025-04-07T14:30:15


โš ๏ธ Important Notes to Remember

  • strftime always returns a string, not a datetime object.
  • The method is case-sensitive: %Y (year) is different from %y (year without century).
  • Any characters in the format string that are not directives (like hyphens, colons, or spaces) appear exactly as written.
  • If you need to include a literal % sign in your output, use %% in the format string.
  • The output depends on the system's locale settings for month and weekday names.

๐Ÿงช Quick Practice Ideas

Try converting a datetime object to these string formats:

  1. "2025-04-07" using "%Y-%m-%d"
  2. "07/04/2025" using "%d/%m/%Y"
  3. "Monday, April 7th 2025" using "%A, %B %dth %Y" (note: ordinal suffixes like "th" are not automatic)
  4. "14:30:15.123456" using "%H:%M:%S.%f"

Remember: strftime is your tool for making dates and times readable and usable in any context where a string is required. The more you practice with different format combinations, the more natural it becomes to build exactly the output you need.


strftime converts a datetime object into a formatted string using special format codes.


๐Ÿงช Example 1: Basic date to string

Converts a full datetime object into a simple date string.

from datetime import datetime

now = datetime(2025, 3, 15, 10, 30, 45)
date_string = now.strftime("%Y-%m-%d")
print(date_string)

๐Ÿ“ค Output: 2025-03-15


๐Ÿงช Example 2: Include time components

Adds hours, minutes, and seconds to the output string.

from datetime import datetime

now = datetime(2025, 3, 15, 10, 30, 45)
time_string = now.strftime("%H:%M:%S")
print(time_string)

๐Ÿ“ค Output: 10:30:45


๐Ÿงช Example 3: Full readable date and time

Creates a human-friendly string with month name, day, year, and time.

from datetime import datetime

now = datetime(2025, 3, 15, 10, 30, 45)
full_string = now.strftime("%B %d, %Y at %I:%M %p")
print(full_string)

๐Ÿ“ค Output: March 15, 2025 at 10:30 AM


๐Ÿงช Example 4: Weekday and abbreviated month

Shows the day of the week and a short month name for reports.

from datetime import datetime

now = datetime(2025, 3, 15, 10, 30, 45)
short_string = now.strftime("%a, %b %d %Y")
print(short_string)

๐Ÿ“ค Output: Sat, Mar 15 2025


๐Ÿงช Example 5: ISO-like timestamp for logs

Formats a timestamp suitable for log files or database entries.

from datetime import datetime

now = datetime(2025, 3, 15, 10, 30, 45)
log_string = now.strftime("%Y-%m-%d %H:%M:%S")
print(log_string)

๐Ÿ“ค Output: 2025-03-15 10:30:45


๐Ÿ“Š Common strftime Format Codes

Code Meaning Example Output
%Y 4-digit year 2025
%m 2-digit month 03
%d 2-digit day 15
%H 24-hour hour 10
%M Minute 30
%S Second 45
%B Full month name March
%b Abbreviated month Mar
%A Full weekday Saturday
%a Abbreviated weekday Sat
%I 12-hour hour 10
%p AM/PM AM

When working with dates and times in Python, you'll often need to convert a datetime object into a human-readable string. This is especially useful when generating log entries, creating timestamps for filenames, or formatting dates for reports. The strftime method (string format time) gives you full control over how the date and time appear.


โš™๏ธ What is strftime?

strftime is a method available on datetime objects that converts them into a formatted string. You provide a format string containing special codes (called directives), and Python replaces each code with the corresponding date or time component.

  • The method is called directly on a datetime object.
  • The format string uses % followed by a letter to represent different date/time parts.
  • The result is always a string that you can print, save, or use in other operations.

๐Ÿ“Š Common Format Directives

Here are the most frequently used format codes for building your date strings:

Directive Meaning Example Output
%Y Year with century (4 digits) 2025
%y Year without century (2 digits) 25
%m Month as zero-padded number 01 to 12
%B Full month name January
%b Abbreviated month name Jan
%d Day of month (zero-padded) 01 to 31
%A Full weekday name Monday
%a Abbreviated weekday name Mon
%H Hour (24-hour clock) 00 to 23
%I Hour (12-hour clock) 01 to 12
%M Minute (zero-padded) 00 to 59
%S Second (zero-padded) 00 to 59
%p AM or PM AM
%f Microsecond (6 digits) 000000 to 999999

๐Ÿ› ๏ธ Basic Usage Examples

Example 1: Simple date string - Create a datetime object: from datetime import datetime then now = datetime.now() - Convert to string: date_string = now.strftime("%Y-%m-%d") - Result: 2025-04-07

Example 2: Full readable format - Format string: "%B %d, %Y" - Result: April 07, 2025

Example 3: Including time - Format string: "%Y-%m-%d %H:%M:%S" - Result: 2025-04-07 14:30:15

Example 4: 12-hour clock with AM/PM - Format string: "%I:%M %p on %A, %B %d" - Result: 02:30 PM on Monday, April 07


๐Ÿ•ต๏ธ Common Patterns for Engineers

Here are some practical format patterns you'll likely use in your daily work:

For log timestamps: - Pattern: "%Y-%m-%d %H:%M:%S" - Output: 2025-04-07 14:30:15

For filenames (no spaces or colons): - Pattern: "%Y%m%d_%H%M%S" - Output: 20250407_143015

For human-readable logs: - Pattern: "%a %b %d %H:%M:%S %Y" - Output: Mon Apr 07 14:30:15 2025

For ISO 8601 format: - Pattern: "%Y-%m-%dT%H:%M:%S" - Output: 2025-04-07T14:30:15


โš ๏ธ Important Notes to Remember

  • strftime always returns a string, not a datetime object.
  • The method is case-sensitive: %Y (year) is different from %y (year without century).
  • Any characters in the format string that are not directives (like hyphens, colons, or spaces) appear exactly as written.
  • If you need to include a literal % sign in your output, use %% in the format string.
  • The output depends on the system's locale settings for month and weekday names.

๐Ÿงช Quick Practice Ideas

Try converting a datetime object to these string formats:

  1. "2025-04-07" using "%Y-%m-%d"
  2. "07/04/2025" using "%d/%m/%Y"
  3. "Monday, April 7th 2025" using "%A, %B %dth %Y" (note: ordinal suffixes like "th" are not automatic)
  4. "14:30:15.123456" using "%H:%M:%S.%f"

Remember: strftime is your tool for making dates and times readable and usable in any context where a string is required. The more you practice with different format combinations, the more natural it becomes to build exactly the output you need.

Interactive Views

You are currently in ๐Ÿ“š All-in-One mode. Use the tabs at the top to switch to ๐Ÿ“– Theory Only or ๐Ÿ’ป Code Only views.

strftime converts a datetime object into a formatted string using special format codes.


๐Ÿงช Example 1: Basic date to string

Converts a full datetime object into a simple date string.

from datetime import datetime

now = datetime(2025, 3, 15, 10, 30, 45)
date_string = now.strftime("%Y-%m-%d")
print(date_string)

๐Ÿ“ค Output: 2025-03-15


๐Ÿงช Example 2: Include time components

Adds hours, minutes, and seconds to the output string.

from datetime import datetime

now = datetime(2025, 3, 15, 10, 30, 45)
time_string = now.strftime("%H:%M:%S")
print(time_string)

๐Ÿ“ค Output: 10:30:45


๐Ÿงช Example 3: Full readable date and time

Creates a human-friendly string with month name, day, year, and time.

from datetime import datetime

now = datetime(2025, 3, 15, 10, 30, 45)
full_string = now.strftime("%B %d, %Y at %I:%M %p")
print(full_string)

๐Ÿ“ค Output: March 15, 2025 at 10:30 AM


๐Ÿงช Example 4: Weekday and abbreviated month

Shows the day of the week and a short month name for reports.

from datetime import datetime

now = datetime(2025, 3, 15, 10, 30, 45)
short_string = now.strftime("%a, %b %d %Y")
print(short_string)

๐Ÿ“ค Output: Sat, Mar 15 2025


๐Ÿงช Example 5: ISO-like timestamp for logs

Formats a timestamp suitable for log files or database entries.

from datetime import datetime

now = datetime(2025, 3, 15, 10, 30, 45)
log_string = now.strftime("%Y-%m-%d %H:%M:%S")
print(log_string)

๐Ÿ“ค Output: 2025-03-15 10:30:45


๐Ÿ“Š Common strftime Format Codes

Code Meaning Example Output
%Y 4-digit year 2025
%m 2-digit month 03
%d 2-digit day 15
%H 24-hour hour 10
%M Minute 30
%S Second 45
%B Full month name March
%b Abbreviated month Mar
%A Full weekday Saturday
%a Abbreviated weekday Sat
%I 12-hour hour 10
%p AM/PM AM