Home > Article > Backend Development > How can I format a datetime object in Python to a human-readable string?
Formatting Datetime Objects in Python
Given a datetime object t, you may desire to convert it into a human-readable string. The datetime class provides a straightforward solution through its strftime() method.
In the example provided:
<code class="python">t = e['updated_parsed'] dt = datetime.datetime(t[0],t[1],t[2],t[3],t[4],t[5],t[6] print dt</code>
dt now holds the datetime information. To format it as "January 28, 2010," you can use the following code:
<code class="python">my_datetime = dt.strftime("%B %d, %Y")</code>
The strftime() method accepts a variety of format specifiers that control the output format. For this particular example:
By combining these format specifiers, you can achieve the desired output of "January 28, 2010."
The above is the detailed content of How can I format a datetime object in Python to a human-readable string?. For more information, please follow other related articles on the PHP Chinese website!