How Much Time Left To Given Date (days, Hours, Mins, S.)
I'm trying to make some kind of 'deadline clock' in python. There is lot of topics about time difference calculations and I followed some and put together this kind of code: import
Solution 1:
Just substract your start date with the end date.
import datetime
today = datetime.date.today()
timenow = datetime.datetime.now()
deadline = "2019-12-12 15:00:00"
current_time = str(today) + " " + str(timenow.strftime("%H:%M:%S"))
start = datetime.datetime.strptime(current_time,'%Y-%m-%d %H:%M:%S')
ends = datetime.datetime.strptime(deadline, '%Y-%m-%d %H:%M:%S')
print(start - ends)
As suggested in the comments, you don't really need to to use both .today()
and .now()
separately, .now()
returns the current date and time as a datetime object itself.
import datetime
timenow = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
deadline = "2019-12-12 00:00:00"
start = datetime.datetime.strptime(timenow,'%Y-%m-%d %H:%M:%S')
ends = datetime.datetime.strptime(deadline, '%Y-%m-%d %H:%M:%S')
print(start - ends)
Post a Comment for "How Much Time Left To Given Date (days, Hours, Mins, S.)"