# 创建日期时间对象,并格式化字符串输出 dt_str = datetime.today().strftime("%Y-%m-%d %H:%M:%S") print(dt_str, type(dt_str)) # 传化成日期对象 dt = datetime.strptime(dt_str, "%Y-%m-%d %H:%M:%S") print(dt, type(dt))
2019-01-10 21:10:43 <class 'str'>
2019-01-10 21:10:43 <class 'datetime.datetime'>
只要日期或者时间可以根据格式化字符串指定。