python的datetime和unix时间戳之间相互转换

时间:2021-04-07 02:30:34

python的datetime和unix时间戳之间相互转换

将python的datetime转换为unix时间戳

import time
import datetime

dtime
= datetime.datetime.now()
ans_time
= time.mktime(dtime.timetuple())

将unix时间戳转换为python的datetime

import datetime

unix_ts
= 1439111214.0
time
= datetime.datetime.fromtimestamp(unix_ts)