How do I turn a python datetime into a string, with readable format date?
The datetime class has a method strftime. The Python docs documents the different formats it accepts:
- Python 2: strftime() Behavior
- Python 3: strftime() Behavior
For this specific example, it would look something like:
my_datetime.strftime("%B %d, %Y")
Convert datetime object to a String of date only in Python
You can use strftime to help you format your date.
E.g.,
import datetime
t = datetime.datetime(2012, 2, 23, 0, 0)
t.strftime('%m/%d/%Y')
will yield:
'02/23/2012'
More information about formatting see here
How to convert a datetime to a string as shown?
You can do it using strptime() to achieve a human-readable string.
import datetime
a = datetime.datetime.now()
print(a.strftime("%a, %d, %B, %Y")) # Sun, 28, February, 2021
print(a.strftime("%A, %d, %B, %Y")) # Sunday, 28, February, 2021
import locale
locale.setlocale(locale.LC_TIME, "sv_SE") # swedish locale
print(a.strftime("%a, %d, %B, %Y")) # sön, 28, februari, 2021
Convert Date time to human readable formate python
Use datetime
module:
from datetime import datetime
d = datetime.strptime('2022-03-21 16:29:01.8593', '%Y-%m-%d %H:%M:%S.%f')
s = d.strftime('%m/%d/%Y %I:%M %p')
Output:
>>> d
datetime.datetime(2022, 3, 21, 16, 29, 1, 859300)
>>> s
'03/21/2022 04:29 PM'
Use this link as documentation.
Python 3 How to format to yyyy-mm-ddThh:mm:ssZ
Try datetime
library
import datetime
output_date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
print(output_date)
For more information, refer to the Python Documentation.
Converting a readable date & time string to a datetime object in Python
The second item in the format string (%m
) refers to month. Changing it to %d
will make your code work.
datetime.datetime.strptime(s,"%b %d, %Y %H:%M:%S")
Here are the relevant parts from strptime
documentation:
%d Day of the month as a zero-padded decimal number. 01, 02, ..., 31
%m Month as a zero-padded decimal number. 01, 02, ..., 12
Python convert date format
An alternative approach using pandas function below:
import pandas as pd
d = pd.to_datetime('2020-08-14')
d.strftime('%d %B %Y')
Out[11]: '14 August 2020'
Convert date to human-readable format (tomorrow, 2 days ago) in Python
Yes, You can use humanize
, It has different set of APIs for Date & time humanization
>>> import humanize
>>> import datetime as dt
>>> humanize.naturalday(dt.datetime.now())
'today'
>>> humanize.naturaldelta(dt.timedelta(seconds=1001))
'16 minutes'
>>> humanize.naturalday(dt.datetime.now() - dt.timedelta(days=1))
'yesterday'
>>> humanize.naturaltime(dt.datetime.now() - dt.timedelta(seconds=1))
'a second ago'
You can also use precisedelta
API to get more accurate representation of your delta.
>>> import humanize
>>> import datetime as dt
>>> delta = dt.timedelta(seconds=3633, days=2, microseconds=123000)
>>> humanize.precisedelta(delta)
'2 days, 1 hour and 33.12 seconds'
>>> humanize.precisedelta(delta, minimum_unit="microseconds")
'2 days, 1 hour, 33 seconds and 123 milliseconds'
>>> humanize.precisedelta(delta, suppress=["days"], format="%0.4f")
'49 hours and 33.1230 seconds'
API reference documentation can be found here.
Related Topics
"Fire and Forget" Python Async/Await
How to Send a Head Http Request in Python 2
Generate 'N' Unique Random Numbers Within a Range
Scope of Lambda Functions and Their Parameters
Circular Import Dependency in Python
Passing an Integer by Reference in Python
Multiprocessing Global Variable Updates Not Returned to Parent
Shooting a Bullet in Pygame in the Direction of Mouse
Create a Directly-Executable Cross-Platform Gui App Using Python
Convert a Number Range to Another Range, Maintaining Ratio
Using Pip Behind a Proxy with Cntlm
What's the Purpose of "Send" Function on Python Generators
Shared-Memory Objects in Multiprocessing
Activate a Virtualenv with a Python Script
Rotate Axis Text in Python Matplotlib
Calling a Python Script with Input Within a Python Script Using Subprocess