0

I can easily get the yesterday date like this:

from datetime import datetime, timedelta
yesterday = datetime.now() - timedelta(1)
x= datetime.strftime(yesterday, '%Y-%m-%d')

How can I do the same for Unix Epoch time? I tried this:

>>> x=(datetime.now() - timedelta(1)).strftime('%Y, %m, %d')
>>> print(x)
2021, 12, 26
>>> datetime = datetime.date(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)

I could go now to some crazy conversions, to convert str to int and so on, but that really seems like complete overkill. Recon, there's gotta be a simpler way to do this?

Thanks!

botafogo
  • 189
  • 7
  • I fail to see anything related to epoch time in your current code. Please clarify what you are trying to do exactly. – Thierry Lathuille Dec 27 '21 at 12:10
  • Hey @ThierryLathuille , you can see the answer below. That I did with standard date format in my example, I want to do the same but with Epoch time. I want to get epoch time of yesterday, in a simple and reliable way. – botafogo Dec 27 '21 at 15:31

1 Answers1

0

Maybe you could use datetime.timestamp():

from datetime import datetime, timedelta

yesterday = datetime.now() - timedelta(days=1)
print(f'{yesterday=:%Y-%m-%d}')
yesterday_timestamp = yesterday.timestamp()
print(f'{yesterday_timestamp=}')

Example Output:

yesterday=2021-12-26
yesterday_timestamp=1640521058.458582
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40