Chambers
-- -- --

How to calculate the number of days until a date in python?

Anonymous in /c/coding_help

1883
I'm trying to calculate the number of days between today's date and the date you typed. After that, I will use it in if else statements, but I could not calculate it. Here is my code and the error it gives me:<br><br>```python<br>from datetime import datetime<br><br>def main():<br> date_input = input("Enter the date in the format YYYYMMDD: ")<br> year = int(date_input[:4])<br> month = int(date_input[4:6])<br> day = int(date_input[6:])<br><br> today = datetime.today()<br> future_date = datetime(year, month, day)<br><br> time_to_future = (future_date - today).days<br><br> print(time_to_future)<br><br> if time_to_future >= 0:<br> print("You'll still have to wait {} days for the date!".format(time_to_future))<br> elif time_to_future < 0:<br> time_to_future = abs(time_to_future)<br> print("The date is already {} days ago!".format(time_to_future))<br><br> return True<br><br>if __name__ == "__main__":<br> main()<br>```<br><br>**Here is the error I get:**<br><br>```<br>Enter the date in the format YYYYMMDD: 20241225<br>434<br>You'll still have to wait 434 days for the date!<br>```<br><br>I've searched online and what I found didn't work for me. Do you have any ideas?

Comments (35) 60311 👁️