To find the number of days between two dates, Python implements

Source: Internet
Author: User

The code is as follows

1 defLeap_year (y):#determine if a leap year2     if(y% 4 = = 0 andY% 100 = 0)orY% 400 = =0:3         returnTrue4     Else:5         returnFalse6         7 defDays_in_month (Y, m):#Judging every month for a few days8     ifMinch[1, 3, 5, 7, 8, 10, 12]:9         return31Ten     elifMinch[4, 6, 9, 11]: One         return30 A     Else: -         ifleap_year (y): -             return29 the         Else: -             return28 -              - defDays_this_year (year):#Judging a few days this year +     ifleap_year (year): -         return366 +     Else: A         return365 at              - defDays_passed (year, Month, day):#Judging how many days have gone by this year -m = 1 -Days =0 -      whileM <Month: -Days + =Days_in_month (year, m) inM + = 1 -     returnDays + Day to  + defdaysbetweendates (year1, Month1, Day1, Year2, Month2, day2): -     ## the     #Your code here. *     ## $     ifYear1 = =year2:Panax Notoginseng         returnDays_passed (Year2, Month2, Day2)-days_passed (year1, Month1, Day1) -     Else: theSUM1 =0 +Y1 =year1 A          whileY1 <year2: theSum1 + =days_this_year (y1) +Y1 + = 1 -         returnSum1-days_passed (year1,month1,day1) +days_passed (year2,month2,day2)

Use the following code for correctness testing

defTest (): Test_cases= [((2012,1,1,2012,2,28), 58),                   ((2012,1,1,2012,3,1), 60),                  ((2011,6,30,2012,6,30), 366),                  ((2011,1,1,2012,8,8), 585 ),                  ((1900,1,1,1999,12,31), 36523)]     for(args, answer)inchTest_cases:result= Daysbetweendates (*args)ifResult! =Answer:Print "Test with data:", args,"failed"        Else:            Print "Test Case passed!"Test ()

The test results are as follows

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.