DJ Far
-1
Q:

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')
1
>>> # 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)
1

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)	
1

New to Communities?

Join the community