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!
CodePudding user response:
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