Calculating the days between two given dates

MukeshRKrish

This coding is to find the days between dates.my coding is not working for the case which i mentioned as comment at last of code, pls specify my error (note : (y1,m1,d1) -> start date and (y2,m2,d2) -> end date)

def days_between_dates(y2,m2,d2,y1,m1,d1):
    days = 0
    tot = 0
    while not(y1==y2 and m1==m2 and d1==d2):
        days = days + 1
        d1 = d1+1
        if((m1 == 4 or m1 == 6 or m1 == 9 or m1 == 11) and d1 == 30):
            d1 = 0
            m1 = m1+1
        if(d1 == 31):
            d1 = 0
            m1 = m1+1
        if (((y1%4)!=0) and m1 == 2 and d1==28):
            d1 = 0
            m1 = m1+1
        else:
            if(m1 == 2 and d1 == 29):
                d1 = 0
                m1 = m1+1
        if(m1>12):
            m1 = 1
            y1 = y1 + 1
        if(y1==y2 and m1==m2 and d1==d2):
            return days
            break
    return days
print days_between_dates(2011,1,1,2010,1,1)
print days_between_dates(2013,1,1,2012,1,1)
#print days_between_dates(2012,2,29,2012,2,28)
Martijn Pieters

Avoid reinventing the wheel, and use the datetime module instead:

from datetime import date

def days_between_dates(y2, m2, d2, y1, m1, d1):
    return (date(y2, m2, d2) - date(y1, m1, d1)).days

As for your error: You are using 0-based date arithmetic; whenever you come to the end of a month, you switch to day 0 of the next month. This means you will never hit the end condition if the y2, m2, d1 day is the last day of the month, for example; before you test for 2012, 2, 29 you already changed the date to 2012, 3, 0.

Use 1-based arithmetic, and only change the month when you get beyond the last day of that month.

Note that you can test equality between tuples, no need to do a full test against each element. Your leap year calculation needs a little refinement too:

def is_leap_year(year):
    if year % 400 == 0:
        return True
    if year % 100 == 0:
        return False
    return year % 4 == 0

def days_between_dates(y2, m2, d2, y1, m1, d1):
    days = 0
    isleapyear = is_leap_year(y1)

    while (y1, m1, d1) != (y2, m2, d2):
        days += 1
        d1 += 1

        if (m1 == 2 and d1 == (30 if isleapyear else 29) or
            m1 in (4, 6, 9, 11) and d1 == 31 or d1 == 32):
            d1 = 1
            m1 += 1

        if m1 == 13:
            m1 = 1
            y1 += 1
            isleapyear = is_leap_year(y1)

    return days

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Elapsed days between two dates always giving one days difference

분류에서Dev

How many days between two dates are in a certain month?

분류에서Dev

SQL to get number of working days between two dates

분류에서Dev

How to get the number of weeks and its respective dates between two given dates (VB.NET)

분류에서Dev

Calculating Time Difference between 2 Dates

분류에서Dev

Calculating the distance between 2 given points

분류에서Dev

How to count fractional days between 2 dates

분류에서Dev

Difference in years between two dates

분류에서Dev

Gap Between two two dates of different cells

분류에서Dev

How to set class between two days to datepicker?

분류에서Dev

how to receive all days between 2 dates in laravel

분류에서Dev

Number of working days between 2 dates according JIRA's configuration

분류에서Dev

sqlite : time difference between two dates in decimals

분류에서Dev

Room availability between two dates (beginner)

분류에서Dev

Difference between two dates in excel 2013

분류에서Dev

How to display all the dates between multiple two dates in a table?

분류에서Dev

Get dates between current date and a given month in postgresql

분류에서Dev

Calculating days from TimeSpan hours

분류에서Dev

How to find data between two days BUT excluding the data in the weekends

분류에서Dev

find all dates between startdate and enddate with a gap of certain number of days in php or javascript

분류에서Dev

How to get difference between 2 dates in number of days,hours,minutes and seconds

분류에서Dev

Joda Time - Calculation of seconds between two dates throws an exception.

분류에서Dev

R - How to subset a table between two specific dates?

분류에서Dev

Mysql how to group by each day between two dates?

분류에서Dev

MDX: How to get the weeks between two dates in MDX Query

분류에서Dev

MySQL query to search in between two time range between two dates using timestamp data

분류에서Dev

Compare two timestamp days

분류에서Dev

How to select last 30 days dates in MySQL?

분류에서Dev

PHP, identify dates and week days in strings

Related 관련 기사

  1. 1

    Elapsed days between two dates always giving one days difference

  2. 2

    How many days between two dates are in a certain month?

  3. 3

    SQL to get number of working days between two dates

  4. 4

    How to get the number of weeks and its respective dates between two given dates (VB.NET)

  5. 5

    Calculating Time Difference between 2 Dates

  6. 6

    Calculating the distance between 2 given points

  7. 7

    How to count fractional days between 2 dates

  8. 8

    Difference in years between two dates

  9. 9

    Gap Between two two dates of different cells

  10. 10

    How to set class between two days to datepicker?

  11. 11

    how to receive all days between 2 dates in laravel

  12. 12

    Number of working days between 2 dates according JIRA's configuration

  13. 13

    sqlite : time difference between two dates in decimals

  14. 14

    Room availability between two dates (beginner)

  15. 15

    Difference between two dates in excel 2013

  16. 16

    How to display all the dates between multiple two dates in a table?

  17. 17

    Get dates between current date and a given month in postgresql

  18. 18

    Calculating days from TimeSpan hours

  19. 19

    How to find data between two days BUT excluding the data in the weekends

  20. 20

    find all dates between startdate and enddate with a gap of certain number of days in php or javascript

  21. 21

    How to get difference between 2 dates in number of days,hours,minutes and seconds

  22. 22

    Joda Time - Calculation of seconds between two dates throws an exception.

  23. 23

    R - How to subset a table between two specific dates?

  24. 24

    Mysql how to group by each day between two dates?

  25. 25

    MDX: How to get the weeks between two dates in MDX Query

  26. 26

    MySQL query to search in between two time range between two dates using timestamp data

  27. 27

    Compare two timestamp days

  28. 28

    How to select last 30 days dates in MySQL?

  29. 29

    PHP, identify dates and week days in strings

뜨겁다태그

보관