我想计算从现在到明天12:00之间的秒数。所以我需要明天12:00 datetime
对象。如何在Python中修改datetime.datetime.hour?
这是伪代码:
today_time = datetime.datetime.now()
tomorrow = today_time + datetime.timedelta(days = 1)
tomorrow.hour = 12
result = (tomorrow-today_time).total_seconds()
但它会引发此错误:
AttributeError: attribute 'hour' of 'datetime.datetime' objects is not writable
如何修改小时或我怎样才能得到一个明天12:00 datetime
对象?
你可以看看[这个答案] HTTP ://stackoverflow.com/questions/8777753/converting-datetime-date-to-utc-timestamp-in-python)所以你可以转换**明天12:00 ** datetime到时间戳,然后减去'time.time( )' – pixis