将设置的日期时间转换为纪元时间
Convert a set datetime to epoch time
需要将每天的时间00:00:00转换为纪元时间,下面是我的代码:
import datetime
today = datetime.datetime.now()
today_morning = today.strftime('%y%m%d') + "000000"
epoch = int(today_morning.strftime('%s'))
print(epoch)
我收到以下错误:
AttributeError: 'str' object has no attribute 'strftime'
如果有人有任何建议,请分享。我查看了其他答案,但它们似乎是针对 Python 2.X 和当前时间,而不是预定时间。我正在寻找 Python 3.X 个答案。
我建议您采用以下解决方案:
import datetime
today = datetime.datetime.now()
today_morning = today.replace(hour=0, minute=0, second=0, microsecond=0)
epoch = int(today_morning.timestamp())
print(epoch)
需要将每天的时间00:00:00转换为纪元时间,下面是我的代码:
import datetime
today = datetime.datetime.now()
today_morning = today.strftime('%y%m%d') + "000000"
epoch = int(today_morning.strftime('%s'))
print(epoch)
我收到以下错误:
AttributeError: 'str' object has no attribute 'strftime'
如果有人有任何建议,请分享。我查看了其他答案,但它们似乎是针对 Python 2.X 和当前时间,而不是预定时间。我正在寻找 Python 3.X 个答案。
我建议您采用以下解决方案:
import datetime
today = datetime.datetime.now()
today_morning = today.replace(hour=0, minute=0, second=0, microsecond=0)
epoch = int(today_morning.timestamp())
print(epoch)