Python counts the number of days between two times and turns the number of days into int

Source: Internet
Author: User
Tags date1

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.