Transformation.
(a) give two recognizable dates, such as MM/DD/YY or DD/MM/YY format. Calculates the number of days between two dates.
(b) Give a person's birthday, counting the number of days from birth to the present, including all leap month.
(c) Also the above example, which calculates how many days the person's next birthday will be.
Answer
(a) The code is as follows:
def date_convert (date_input): month = Int (date_input.split ('/') [0]) Day = Int (date_input.split ('/') [1] Year = Int. (' + ' + date_input.split ('/') [2]) return (year, month, day) import datetimedate_input = Raw_ Input (' Please input the begin date, mm/dd/yy ... ') D1 = datetime.date (Date_convert (Date_input) [0], Date_convert (Date_input) [1], Date_convert (Date_input) [2]) Date_ input = raw_input (' Please input the end date, mm/dd/yy ... ') D2 = Datetime.date (Date_convert (Date_input) [0], Date_convert (Date_input) [1], Date_convert (Date_input) [2]) print ( D2-D1). Days
(b) The code is as follows:
def date_convert (date_input): month = Int (date_input.split ('/') [0]) Day = Int (date_input.split ('/') [1] Year = Int (date_input.split ('/') [2]) return (year, month, day) Import datetimedate_input = Raw_input (' Please input his birthday, mm/dd/yyyy ... ') D1 = datetime.date (Date_convert (Date_input) [0], Date_convert (Date_input) [1], Date_convert (Date_input) [2]) print ( Datetime.date.today ()-D1). Days
(c) The code is as follows:
def date_convert (date_input): month = Int (date_input.split ('/') [0]) Day = Int (date_input.split ('/') [1] Year = Int (date_input.split ('/') [2]) return (year, month, day) Import datetimedate_input = Raw_input (' Please input his birthday, mm/dd/yyyy ... ') Import timenext_year = Int (time.strftime ('%Y ', Time.localtime (Time.time ()))) + 1next_birthday = datetime.date (next_ Year, Date_convert (Date_input) [1], Date_convert (Date_input) [2]) print (Next_birthday-datetime.date.today ()). Days
The above code support 06/13/15 does not support 06/13/2015
Python core Programming chapter sixth Exercise 6-15