Import TimeImportdatetime#calculates two date difference days, a custom function name, and a variable name of two dates. defCaltime (date1,date2):#%y-%m-%d is a date format in which the--can be substituted or not written, but unified, the same as the seconds after the same; you can only calculate dates, not time. #Date1=time.strptime (date1, "%y-%m-%d%h:%m:%s") #Date2=time.strptime (Date2, "%y-%m-%d%h:%m:%s")Date1=time.strptime (Date1,"%y/%m/%d") Date2=time.strptime (Date2,"%y/%m/%d") #depending on whether you want to calculate the date or date time, you need to determine the number of array segments. Subscript 0 represents the year, the small Mark 1 represents the month, and so on ... #Date1=datetime.datetime (date1[0],date1[1],date1[2],date1[3],date1[4],date1[5]) #Date2=datetime.datetime (date2[0],date2[1],date2[2],date2[3],date2[4],date2[5])Date1=datetime.datetime (date1[0],date1[1],date1[2]) Date2=datetime.datetime (date2[0],date2[1],date2[2]) #returns the difference between the two variables, which is the difference in number of days Print((date2-date1). Days)#turn the number of days into int type return(date2-date1)if __name__=='__main__': Print('Please enter an earlier date (format example: XXXX/XX/XX):') Dt1=input ()Print('\ n Please enter a later date (in the format: XXXX/XX/XX):') DT2=input ()Print('the earlier date is:'+dt1+'The later date is:'+DT2)Print('\ n Two date differences') Caltime (DT1,DT2)Print(Caltime (DT1,DT2))Print(Type (Caltime (DT1,DT2)))
Python counts the number of days between two times and turns the number of days into int