Python: Find the number of days that have passed between two dates accounting for leap years

Another of the challenges in Udacity’s Introduction to Computer Science.

This is probably the most challenging thing I’ve done so far, other than the naughts and crosses game. Honestly, it took forever. I got stuck a million times. My logic failed a trillion times. Oh, the frustration.

Still, I managed to get it working and now I’m very pleased, so am going to take a break and have a cup of tea.


# Given your birthday and the current date, calculate your age in days. 
# Account for leap days. 
#
# Assume that the birthday and current date are correct dates (and no 
# time travel). 
#

daysOfMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

# Return True if a given year is a leap year
# Return False if not

def isLeapYear(n):
    if (n % 4 == 0 and n % 100 != 0) or n % 400 == 0:
        return True
    else:
        return False
    
# This function takes 6 values as input.
# birthyear, birthmonth and birthday are
# compared to currentyear, currentmonth
# and currentday in order to find out the
# numbner of days which have passed between
# the two dates.

def daysBetweenDates(birthYear, birthMonth, birthDay, currentYear, currentMonth, currentDay):
    
    # Set days to zero.
    days = 0

    # This loop will add 365 days
    # to the variable days for each year
    # which is not leap and 365 days for
    # each year which is leap between
    # birthyear and current year.
    if birthYear != currentYear:
        for x in range(birthYear,currentYear):
            if isLeapYear(x) == True:
                days += 366
            else:
                days += 365

    # Find out how many days have passed
    # in birthyear and currentyear by
    # summing the valuea in daysOfMonths
    # up to the index which represents
    # birthmonth/currentmonth.
    
    daysThisYear = birthDay+sum(daysOfMonths[0:birthMonth-1])
    daysCurrentYear = currentDay+sum(daysOfMonths[0:currentMonth-1])

    # Find how many days have passed
    # between the month and day given for
    # birthyear and the month and day
    # given for currentyear.
    
    days = days+daysCurrentYear-daysThisYear

    # Add a day if birthyear is a leap
    # year and if the date given for this
    # year is Feb 29 or later.

    if isLeapYear(birthYear):
        if (birthMonth == 2 and birthDay == 29) or birthMonth >= 3:
            days=days+1
            
    # Add a day if currentyear is a leap
    # year and if the date given for this
    # year is Feb 29 or later.

    if isLeapYear(currentYear):
        if (currentMonth == 2 and currentDay == 29) or currentMonth >= 3:
            days=days+1

    return days

def test():
    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) in test_cases:
        result = daysBetweenDates(*args)
        if result != answer:
            print "Test with data:", args, "failed"
        else:
            print "Test case passed!"

test()
About these ads

4 thoughts on “Python: Find the number of days that have passed between two dates accounting for leap years

  1. Pingback: riddle 2 (python) | ferretfarmer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s