Get today's countdown (remaining seconds) using python¶
An activity will be over today, and I want to show users the countdown so they can hurry up.
Python Code¶
from datetime import datetime
from datetime import time as dt_time
from datetime import timedelta
def get_today_countdown() -> int:
now = datetime.now()
# zero o'clock in tomorrow's morning
next_day = datetime.combine(now + timedelta(days=1), dt_time.min)
return int((next_day - now).total_seconds())
# test
print(f'The remaining seconds today: {get_today_countdown()}')
print(f'The remaining hours today: {get_today_countdown() / 3600:.2f}')
Output example:
The remaining seconds today: 13461
The remaining hours today: 3.74
This article is originally created by tooli.top. Please indicate the source when reprinting : https://www.tooli.top/posts/python_today_countdown
Posted on 2022-06-17
Mail to author