-1

I am trying to calculate the number of days between two dates where the user is inputing the dates but cannot find how to do it. I currently have this code:

from datetime import date

startdate = date(input("When do you intend to start?"))
enddate = date(input("When is your deadline?"))
numstudydays = enddate - startdate
print (numstudydays)

Thank you

Squigss
  • 13
  • 6

3 Answers3

1

In python, input() method accepts the input as a string variable. You can then typecast it into an integer using int() method. But you cannot typecast an entire tuple. i.e (year, month, day) in your case. You can do it by getting all the values one by one.

from datetime import date

print("Enter intended start date details: ")
year = int(input('Year: '))
month = int(input('Month: '))
day = int(input('Day: '))
startdate = date(year, month, day)

print("Enter intended enddate details: ")
year = int(input('Year: '))
month = int(input('Month: '))
day = int(input('Day: '))
enddate = date(year, month, day)
numstudydays = enddate - startdate

print (numstudydays)

Hope that helps.

  • This answer is not inherently wrong, but it doesn't perform data validation by checking that month days are out of range, for example. The [datetime](https://docs.python.org/release/3.6.2/library/datetime.html#strftime-and-strptime-behavior) module documents more standard and proven ways of achieving that. – Acsor Oct 10 '17 at 07:33
0

I think your biggest issue is how you retrieve the date. date() function requires three integers, e.g. date(2017,10,10)

When you use input() you get a string:

input()  #returns a string

You would also need to solve this by splitting the input somehow:

e.g.

"2017-10-12".split("-") # returns ["2017","10","12"]
[int(i) for i in "2017-10-12".split("-")] # returns [2017,10,12]

That means if you do this, you get a date:

date(*[int(i) for i in input("When is your deadline?").split("-")])

But what you really want in this case is to use strptime. Strptime() converts a string to a datetime by defining how it looks:

from datetime import datetime
datetime.strptime("20171012","%Y%m%d").date() # returns: datetime.date(2017, 10, 12)

Example of full script

from datetime import datetime

startdate_str = input("When do you intend to start? e.g.(2017-10-10)")
enddate_str = input("When is your deadline? e.g.(2017-10-11)")

startdate_dt = datetime.strptime(startdate_str,"%Y-%m-%d").date()
enddate_dt = datetime.strptime(enddate_str,"%Y-%m-%d").date()

numstudydays = enddate_dt - startdate_dt

print (numstudydays.days) # prints 1 for example inputs

Recommended links

The various kind of ways to format a string: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

Anton vBR
  • 18,287
  • 5
  • 40
  • 46
0

You are making an incorrect use of the datetime constructor. I revised your solution to handle user input in a simple and manageable way. A basic data validation is performed by the datetime.strptime factory method but there could be more (like checking, for example, that the end date always comes after the start date):

from datetime import datetime, date

start, end = None, None

while start is None:
    rawinput = input("When do you intend to start? ")

    try:
        # %Y stands for a fully specified year, such as 2017, 1994, etc.
        # %m for the integer representation of a month, e.g. 01, 06, 12
        # and %d for the day of the month
        # see https://docs.python.org/3/library/time.html#time.strftime for more
        start = datetime.strptime(rawinput, "%Y %m %d")
    except ValueError:
        print("Input must be \"<year number> <month number> <day number>\"")

while end is None:
    rawinput = input("When is your deadline? ")

    try:
        end = datetime.strptime(rawinput, "%Y %m %d")
    except ValueError:
        print("Input must be \"<year number> <month number> <day number>\"")

diff = end - start

print("You have %d days of time" % diff.days)

I did more than a couple of tests, and this is what resulted:

None@vacuum:~$ python3.6 ./test.py 
When do you intend to start? 2017 01 32
Input must be "<year number> <month number> <day number>"
When do you intend to start? 2017 01 31
When is your deadline? 2017 02 30
Input must be "<year number> <month number> <day number>"
When is your deadline? 2017 02 28
You have 28 days of time
Acsor
  • 1,011
  • 2
  • 13
  • 26