bugbiter
bugbiter

Reputation: 1

Using dates from user input with datetime to calculate number of days between the dates

Read dates from user input in the form yyyy,mm,dd. Then find number of days between dates. Datetime wants integers and input needs to be converted from string to integer. Previous questions seem to involve time or a different OS and those suggestions do not seem to work with Anaconda and Win10. I tried those which seem to be for a Win10 OS. The user input variables do not work as entered since they are strings and apparently need to be converted to an integer format.

I've tried various ways to convert to integer and datetime responds with errors indicating it needs integer and is seeing tuple, list or string. If I input the date values directly, everything works fine. I've tried - as a separator in the user input and still get an error. I've tried wrapping the input in (" "), " ", ' ' and ( ) and still get errors. I was not able to be more specific in the tags but the following may help with responses. I am using python 2.6 on Windows 10 in Anaconda. For some reason datetime.strptime is not recognized so some of the responses did not work.

   InitialDate = input("Enter the begin date as yyyy,mm,dd")
   FinalDate = input("Enter the end date as yyyy,mm,dd")
   ID = InitialDate.split(",")
   ID2 = int(Id[0]),int(Id[1]),int(Id[2])
   Iday = datetime.datetime(Id2)
   Fd = FinalDate.split(",")
   Fd2 = int(Fd[0]),int(Fd[1]),int(Fd[2])
   Fday = datetime.datetime(Fd2)
   age = (Fd2 - Id2).day

I expect an integer value for age.
I get type error an integer is required (got type tuple) before execution of the age line.

Upvotes: 0

Views: 789

Answers (3)

pbb.business
pbb.business

Reputation: 21

I just had a similar problem, my solution was to make another int with the value of the array. Something like this:

ID = InitialDate.split(",")
 Id0 = Id[0]
 Id1 = Id[1]
 Id2 = Id[2]
 ID2 = int(Id0),int(Id1),int(Id2)
 Iday = datetime.datetime(Id2)
 Fd = FinalDate.split(",")
 Fd0 = Fd[0]
 Fd1 = Fd[1]
 Fd2 = Fd[2]
 Fd2 = int(Fd0),int(Fd1),int(Fd2)
 Fday = datetime.datetime(Fd2)
 age = (Fd2 - Id2).day 

Upvotes: 0

Stephan Semerad
Stephan Semerad

Reputation: 91

from datetime import datetime

InitialDate = input("Enter the begin date as yyyy/mm/dd: ") FinalDate = input("Enter the end date as yyyy/mm/dd: ")

InitialDate = datetime.strptime(InitialDate, '%Y/%m/%d') FinalDate = datetime.strptime(FinalDate, '%Y/%m/%d')

difference = FinalDate - InitialDate print(difference.days)

Upvotes: 0

Rakesh
Rakesh

Reputation: 82765

Use .strptime() to convert string date to date object and then calculate diff

Ex:

import datetime

InitialDate = "2019,02,10"  #input("Enter the begin date as yyyy,mm,dd")
FinalDate  = "2019,02,20"   #input("Enter the end date as yyyy,mm,dd")

InitialDate = datetime.datetime.strptime(InitialDate, "%Y,%m,%d")
FinalDate = datetime.datetime.strptime(FinalDate, "%Y,%m,%d")
age = (FinalDate - InitialDate).days
print(age)

Output:

10

Upvotes: 2

Related Questions