1

Need to convert the time 00:00:00 of each day to epoch time, below is the code I have:

import datetime
today = datetime.datetime.now()
today_morning = today.strftime('%y%m%d') + "000000"
epoch = int(today_morning.strftime('%s'))
print(epoch)

I get the following error:

AttributeError: 'str' object has no attribute 'strftime'

If anyone has any advice, please share it. I have looked around at other answers, but they seem to be for Python 2.X and for the current time, not a predetermined time. I am looking for Python 3.X answers.

Sam P
  • 37
  • 4
  • 4
    Possible duplicate of [Convert python datetime to epoch with strftime](https://stackoverflow.com/questions/11743019/convert-python-datetime-to-epoch-with-strftime) – Rakesh May 30 '18 at 10:52
  • That's for python 2.x – Sam P May 30 '18 at 12:34

1 Answers1

2

I suggest you the following solution:

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)
Laurent H.
  • 6,316
  • 1
  • 18
  • 40