import datetime
date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')
print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
# app.py
from datetime import datetime
date_str = '10-27-2020'
dto = datetime.strptime(date_str, '%m-%d-%Y').date()
print(type(dto))
print(dto)
>>> import datetime
>>> datetime.datetime.strptime('24052010', "%d%m%Y").date()
datetime.date(2010, 5, 24)
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
from datetime import datetime, timezone
timestamp_str = str(datetime.now(timezone.utc))
print(timestamp_str, type(timestamp_str)) # 2022-05-06 11:23:00.718012+00:00 <class 'str'>
timestamp = datetime.fromisoformat(timestamp_str) # Python 3.7+:
print(timestamp, type(timestamp)) # 2022-05-06 11:23:00.718012+00:00 <class 'datetime.datetime'>
timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S.%f%z')
print(timestamp, type(timestamp)) # 2022-05-06 11:23:00.718012+00:00 <class 'datetime.datetime'>
# import the datetime module
import datetime
# datetime in string format for may 25 1999
input = '2021/05/25'
# format
format = '%Y/%m/%d'
# convert from string format to datetime format
datetime = datetime.datetime.strptime(input, format)
# get the date from the datetime using date()
# function
print(datetime.date())
from dateutil.parser import parse
parse('31, March 31, 2010, 10:51pm')
import datetime
t = datetime.datetime(2012, 2, 23, 0, 0)
t.strftime('%m/%d/%Y')
#Output
'02/23/2012'