Skip to content

Instantly share code, notes, and snippets.

@eduardogspereira
Created June 28, 2020 18:40
Show Gist options
  • Save eduardogspereira/585d94f892de223051ae1c64abfe1b5a to your computer and use it in GitHub Desktop.
Save eduardogspereira/585d94f892de223051ae1c64abfe1b5a to your computer and use it in GitHub Desktop.
class Date:
def __init__(self, day, month, year):
self.day = day
self.month = month
self.year = year
my_birthday = Date(16, 4, 1991)
today = Date(28, 6, 1991)
def make_days_of_months(is_leap_year):
days_of_months = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
if is_leap_year:
days_of_months[1] = 29
return days_of_months
def is_leap_year(year):
if year % 4. != 0.:
return False
elif year % 100 != 0:
return True
elif year % 400 != 0:
return False
else:
return True
def calculate_completed_year_days(date):
days_of_month = make_days_of_months(is_leap_year(date.year))
completed_days = date.day
for month in range(0, date.month - 1):
completed_days += days_of_month[month]
return completed_days
def total_days_in_year(year):
if is_leap_year(year):
return 366
return 365
def days_between_dates(start, end):
initial_date_completed_days = calculate_completed_year_days(start)
final_date_completed_days = calculate_completed_year_days(end)
if start.year == end.year:
return final_date_completed_days - initial_date_completed_days
total_days = 0
for year in range(start.year + 1, end.year):
total_days += total_days_in_year(year)
total_days = total_days +\
(total_days_in_year(start.year) - initial_date_completed_days) +\
final_date_completed_days
return total_days
print(days_between_dates(my_birthday, today))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment