Python datetime 字符串 相互转 datetime
字符串转 datetime
from datetime import datetime
# 定义要转换的日期时间字符串
dt_str = "2021-09-30 15:48:36"
# 使用datetime.strptime()函数进行转换
dt_obj = datetime.strptime(dt_str, "%Y-%m-%d %H:%M:%S")
print(dt_obj)
datetime 转字符串
from datetime import datetime
str_now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(str_now)