I want to get the date time object for last hour. Lets say the sys time is "2011-9-28 06:11:30" I want to get the output as "2011-9-28 05" #{06 - 1 hour}
我想获取最后一小时的日期时间对象。可以说系统时间是“2011-9-28 06:11:30”我希望输出为“2011-9-28 05”#{06 - 1小时}
I used:
lastHourDateTime = date.today() - timedelta(hours = 1)
print lastHourDateTime.strftime('%Y-%m-%d %H:%M:%S')
However, my output is not showing the time part at all. where am I going wrong?
但是,我的输出根本没有显示时间部分。我哪里错了?
2 个解决方案
#1
59
Date doesn't have the hour - use datetime:
日期没有小时 - 使用日期时间:
from datetime import datetime, timedelta
last_hour_date_time = datetime.now() - timedelta(hours = 1)
print last_hour_date_time.strftime('%Y-%m-%d %H:%M:%S')
#2
16
This works for me:
这对我有用:
import datetime
lastHourDateTime = datetime.datetime.now() - datetime.timedelta(hours = 1)
print lastHourDateTime.strftime('%Y-%m-%d %H')
# prints "2011-09-28 12" which is the time one hour ago in Central Europe
#1
59
Date doesn't have the hour - use datetime:
日期没有小时 - 使用日期时间:
from datetime import datetime, timedelta
last_hour_date_time = datetime.now() - timedelta(hours = 1)
print last_hour_date_time.strftime('%Y-%m-%d %H:%M:%S')
#2
16
This works for me:
这对我有用:
import datetime
lastHourDateTime = datetime.datetime.now() - datetime.timedelta(hours = 1)
print lastHourDateTime.strftime('%Y-%m-%d %H')
# prints "2011-09-28 12" which is the time one hour ago in Central Europe