from datetime import datetime as dt
tstr = '2012-12-29 13:49:37'
tdatetime = dt.strptime(tstr, '%Y-%m-%d %H:%M:%S')
The second argument of strptime passes the format of the first argument.
For example
If tstr = '2012/12/29 13:49:37'
,
dt.strptime(tstr, '%Y/%m/%d %H:%M:%S'
)
import datetime
tstr = '2012-12-29 13:49:37'
tdatetime = datetime.datetime.strptime(tstr, '%Y-%m-%d %H:%M:%S')
tdate = datetime.date(tdatetime.year, tdatetime.month, tdatetime.day)
from datetime import datetime as dt
tdatetime = dt.now()
tstr = tdatetime.strftime('%Y/%m/%d')
reference If you want to know other specifiers such as week (like% Y), please check from the bottom of the link below Basic date and time type
Recommended Posts