-1

Bit of a newbie and I have Google and found no joy with this one. I am trying to create a bit of code that will allow a user to input a date and then tell them how many days it is until then. I have tried to convert the string to date.time using strp but I just get an error message.

I want it to run: user inputs date message comes up saying there are XX days until.

Cheers

  • 3
    Welcome to StackOverflow! There are many examples of python `datetime.strptime` online. If you are getting an error please [edit] your question to include it and the code you've used. Thanks! – OneCricketeer Jun 10 '16 at 14:24
  • http://stackoverflow.com/questions/127803/how-to-parse-an-iso-8601-formatted-date-in-python – Marc B Jun 10 '16 at 14:37

1 Answers1

0

Is it what you are searching for? You can parse a string to a datetime with strptime, and then compute the number of days to reach it with timedelta.

from datetime import datetime, timedelta

date = input('Enter your date: ')
delta = datetime.strptime(date, '%Y-%m-%d') - datetime.now()
print("We'll be {} in {} days".format(date, delta.days))

You can of course change the accepted format for the date input by the user, which I set to %Y-%m-%d in the example here above.

combefis
  • 412
  • 1
  • 5
  • 11