luscgu00
luscgu00

Reputation: 43

Time to next full hour

I have a timestamp like the following:

Timestamp('2022-01-01 19:55:00')

How do i get the time difference to the next full hour (in minutes)?

So in this example the output should be 5 minutes.

Upvotes: 0

Views: 274

Answers (1)

executable
executable

Reputation: 3590

You'll need to use the datetime lib :

from datetime import datetime, timedelta

def time_to_next_hour(timestamp):
    current_time = datetime.strptime(str(timestamp), '%Y-%m-%d %H:%M:%S')
    next_hour = current_time.replace(microsecond=0, second=0, minute=0) + timedelta(hours=1)
    time_diff = next_hour - current_time
    return int(time_diff.total_seconds() / 60)

print(time_to_next_hour(Timestamp('2022-01-01 19:55:00')))

Upvotes: 1

Related Questions