Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to convert datetime to readable string in Python

In Python, you can convert a datetime object to a readable string using the strftime() method. The strftime() method allows you to format a datetime object as a string based on a given format string containing placeholders.

Here's an example:

from datetime import datetime

# Create a datetime object
dt = datetime(2023, 5, 2, 14, 30)

# Format the datetime object as a readable string
formatted_dt = dt.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_dt)

Output:

2023-05-02 14:30:00

In the example above, the format string "%Y-%m-%d %H:%M:%S" contains placeholders like %Y, %m, %d, %H, %M, and %S that represent the year, month, day, hour, minute, and second, respectively. You can customize the format string to display the datetime object in your desired format.

Here are some common placeholders you can use with strftime():

  • %Y: Year with century (e.g., 2023)
  • %y: Year without century (e.g., 23)
  • %m: Month as a zero-padded decimal number (e.g., 05)
  • %B: Month as a full name (e.g., May)
  • %b: Month as an abbreviated name (e.g., May)
  • %d: Day of the month as a zero-padded decimal number (e.g., 02)
  • %A: Weekday as a full name (e.g., Tuesday)
  • %a: Weekday as an abbreviated name (e.g., Tue)
  • %H: Hour (24-hour clock) as a zero-padded decimal number (e.g., 14)
  • %I: Hour (12-hour clock) as a zero-padded decimal number (e.g., 02)
  • %M: Minute as a zero-padded decimal number (e.g., 30)
  • %S: Second as a zero-padded decimal number (e.g., 00)
  • %p: Locale's equivalent of either AM or PM (e.g., PM)
  1. Python convert datetime to string:

    from datetime import datetime
    
    current_datetime = datetime.now()
    datetime_string = str(current_datetime)
    print(datetime_string)
    
  2. Format datetime as string in Python:

    current_datetime = datetime.now()
    formatted_string = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
    print(formatted_string)
    
  3. Convert datetime to custom string format in Python:

    current_datetime = datetime.now()
    custom_format = "%A, %B %d, %Y %I:%M %p"
    custom_string = current_datetime.strftime(custom_format)
    print(custom_string)
    
  4. Python datetime to ISO 8601 string:

    current_datetime = datetime.now()
    iso_string = current_datetime.isoformat()
    print(iso_string)
    
  5. Datetime to string with date and time in Python:

    current_datetime = datetime.now()
    date_time_string = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
    print(date_time_string)
    
  6. Python f-string for datetime formatting:

    current_datetime = datetime.now()
    formatted_string = f"{current_datetime:%Y-%m-%d %H:%M:%S}"
    print(formatted_string)
    
  7. String to datetime conversion in Python:

    date_string = "2023-01-01"
    converted_datetime = datetime.strptime(date_string, "%Y-%m-%d")
    print(converted_datetime)
    
  8. Python datetime to string with timezone:

    from datetime import datetime, timezone, timedelta
    
    current_datetime = datetime.now(timezone.utc)
    formatted_string = current_datetime.astimezone(timezone(timedelta(hours=5.5))).strftime("%Y-%m-%d %H:%M:%S %Z")
    print(formatted_string)
    
  9. Convert datetime to string with day and month names in Python:

    current_datetime = datetime.now()
    custom_format = "%A, %B %d, %Y %I:%M %p"
    custom_string = current_datetime.strftime(custom_format)
    print(custom_string)
    
  10. Python datetime to string with milliseconds:

    current_datetime = datetime.now()
    milliseconds_string = current_datetime.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
    print(milliseconds_string)
    
  11. Datetime to string with specific format and locale in Python:

    from datetime import datetime
    import locale
    
    locale.setlocale(locale.LC_TIME, 'en_US.UTF-8')  # Set to your desired locale
    current_datetime = datetime.now()
    formatted_string = current_datetime.strftime("%A, %B %d, %Y %I:%M %p")
    print(formatted_string)
    
  12. Python datetime to string with day of the week:

    current_datetime = datetime.now()
    day_of_week_string = current_datetime.strftime("%A")
    print(day_of_week_string)
    
  13. Datetime to string with ordinal day in Python:

    current_datetime = datetime.now()
    ordinal_day_string = current_datetime.strftime("%A, %B %d, %Y %I:%M %p").replace("0", "")
    print(ordinal_day_string)
    
  14. Convert datetime to string and back in Python:

    from datetime import datetime
    
    current_datetime = datetime.now()
    formatted_string = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
    converted_datetime = datetime.strptime(formatted_string, "%Y-%m-%d %H:%M:%S")
    print(converted_datetime)