John
John

Reputation: 167

Generate 1 min time interval from current time in python

I have a scenario where I want to generate 1 min time difference from current time past in epoch(seconds) and save them in two int type variables for example t1 (current time) & t2 (time one min ago). How do I acheive that ?

I have explored date time module but not sure how to do that.

Upvotes: 2

Views: 1479

Answers (2)

rahlf23
rahlf23

Reputation: 9019

Try this:

from datetime import datetime

t1 = int(datetime.now().timestamp())
t2 = t1 - 60

Upvotes: 3

buran
buran

Reputation: 14233

from datetime import datetime, timedelta

now = datetime.now()
print(now)
one_minute_ago = now - timedelta(seconds=60)
print(one_minute_ago)

# or directly
print(datetime.now() - timedelta(seconds=60))

output

2020-03-21 17:59:14.315156
2020-03-21 17:58:14.315156
2020-03-21 17:58:14.3152

EDIT: in epoch format

print(int(one_minute_ago.timestamp()))
# or
print(int((datetime.now() - timedelta(seconds=60)).timestamp()))

Upvotes: 1

Related Questions