简体   繁体   中英

How can I subtract a datetime.now object with a datetime string?

I have a server bot where I want to make a certain command usable only once every 24 hours. Each time the user uses the command, I store datetime.now in my SQL database in %H:M:%S format so the variable may looks like this:

b = "07:17:45"

I would like to somehow subtract that stored string with datetime.datetimenow() each time the user uses the command to get an output of the remaining time left to check if 24 hours has passed or not.

You may want to use strptime,

from datetime import datetime
now = datetime.now()
then = datetime.strptime('07:17:45', '%H:%M:%S')
then = datetime.combine(now.date(), then.time())

delta = now - then
time_in_seconds = delta.total_seconds()
time_in_minutes = time_in_seconds / 60
time_in_hours = time_in_minutes / 60

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM