nick nee
nick nee

Reputation: 33

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.

Upvotes: 2

Views: 655

Answers (1)

Reck
Reck

Reputation: 1436

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

Upvotes: 3

Related Questions