Python 日期时间差与日期差为0秒
我试图在python中获取两个日期之间的小时数,第一个是输入日期,第二个是来自miniseed文件名的日期Python 日期时间差与日期差为0秒,python,Python,我试图在python中获取两个日期之间的小时数,第一个是输入日期,第二个是来自miniseed文件名的日期 dt = datetime.strptime("2019-04-15T00:37:00", '%Y-%m-%dT%H:%M:%S') //dt is the input date for filename in os.listdir('.'): if filename.endswith(".mseed"):
dt = datetime.strptime("2019-04-15T00:37:00", '%Y-%m-%dT%H:%M:%S')
//dt is the input date
for filename in os.listdir('.'):
if filename.endswith(".mseed"):
file_struct = filename.split('.')
datetime_object = datetime.strptime(file_struct[0], '%Y%m%d%H%M%S')
hours = abs((dt- datetime_object).seconds)//3600
print(dt, datetime_object, hours)
continue
else:
continue
结果是:
(datetime.datetime(2019,4,15,0,37,10),'20190405082044',16)
(datetime.datetime(2019,4,15,0,37,10),'20190405000000',0)
(datetime.datetime(2019,4,15,0,37,10),'20190405083710',16)
看起来20190405000000(他的datetime对象是datetime.datetime(2019,4,5,0,0))对于总已用秒数,使用
总秒数
而不是秒数
hours = abs((dt -datetime_object).total_seconds()) // 3600
请注意,total_seconds
是一个方法,因此使用括号