Profile
About
How to Convert Python's .isoformat() String Back Into Datetime Object?
.isoformat() is a fantastic tool for transforming any Python DateTime object into an ISO 8601 formatted string. This can be very useful for programmers who want to display dates and times in a specific format without having to Convert Python .isoformat() String Back Into Datetime through the effort of formatting it in their own code.
python DateTime to string iso format
from datetime import datetime
some_date = datetime.now()
iso_date_string = some_date.isoformat()
# For older version of python
iso_date_string = some_date.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
python strftime iso 8601
>>> # convert string in iso 8601 date dime format to python datetime type
>>> import datetime
>>> datetime.datetime.strptime('2020-06-19T15:52:50Z', "%Y-%m-%dT%H:%M:%SZ")
datetime.datetime(2020, 6, 19, 15, 52, 50)
datetime.strttime() syntax
from datetime import datetime
now = datetime.now() # current date and time
year = now.strftime("%Y")
print("year:", year)
month = now.strftime("%m")
print("month:", month)
day = now.strftime("%d")
print("day:", day)
time = now.strftime("%H:%M:%S")
print("time:", time)
date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:",date_time)
The .isoformat() method returns a readable date using the iso-8601 date format. This is useful for giving to users instead of relying on them to enter the date in this format.