Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Calculate the number of days between two dates entered from keyboard
- from datetime import date
- def getdate(stra):
- while True:
- s1 = input("Enter " + stra + " date as yyyy-mm-dd: ")
- ymd = makedate(s1) # validate the input string, and make a date tuple
- if ymd:
- return ymd
- print("Invalid format, try again")
- def makedate(inp):
- splitd = inp.split('-') # Split string on hyphens
- if len(splitd) != 3: # there should be 3 sections
- return None
- # check each section is numeric (more validation could be done here!!)
- if splitd[0].isdigit() and splitd[1].isdigit() and splitd[2].isdigit():
- # extract the year, month, and day
- y = int(splitd[0])
- m = int(splitd[1])
- d = int(splitd[2])
- return y,m,d
- return None
- s1 = getdate("first")
- s2 = getdate("second")
- res1 = makedate(s1)
- res2 = makedate(s2)
- d1 = date(res1[0], res1[1], res1[2])
- d2 = date(res2[0], res2[1], res2[2])
- delta = d2 - d1
- print(delta.days)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement