我想在python中将datetime对象转换为int:
import datetime
time_entered = datetime.datetime.strptime(raw_input("Time1: "), "%H%M")
time_left = datetime.datetime.strptime(raw_input("Time2"), "%H%M")
time_taken = time_left - time_entered
int(time_taken)
当我运行该代码时,我收到以下错误:
TypeError:int()参数必须是字符串或数字,而不是 'datetime.timedelta'
答案 0 :(得分:16)
您可以将datetime对象转换为timetuple,然后使用time.mktime函数
import time
from datetime import datetime
timestamp = int(time.mktime(datetime.now().timetuple()))
将其转换回:
now = datetime.fromtimestamp(timestamp)
答案 1 :(得分:0)
将分钟数转换为float / int可以通过以下方式完成:
time_taken.total_seconds()*60