# By Websten from forums # # 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). # # year1 - дата рождения # year2 - текущая дата def daysBetweenDates(year1, month1, day1, year2, month2, day2): year=year2-year1 month=month2-month1 day=day2-day1 age=day+month*30+year*365 #print '1 ',age year3=year1 i400,i100,i4=0,0,0 while year3<=year2: if year3%400==0: i400=i400+1 if year3%100==0: i100=i100+1 if year3%4==0: i4=i4+1 year3=year3+1 age=age+i4-i100+i400 if month2 == 2 or month1 == 2: if day2 <29 or day1 <29: age=age-1 #while year3<year2: # if year3%400==0: # age=age+1 # if year3%4==0: # age=age+1 # year3=year3+1 # # print '2 ',age #if year1%4==0 or year1%400==0: # if year1% # if month1>2: # age=age+1 # print '3 ',age # if year2%4==0 or year2%400==0: # if month2>2: # age=age+1 # #print '4 ',age month3=month1 if month2>month1: while month3<month2: if month3==1 or month3==3 or month3==5 or month3==7 or month3==8 or month3==10 or month3==12: age=age+1 #print '5 ',age, month3 if month3==2: age=age-2 #print '6 ',age month3=month3+1 # #print '7 ',age if month2<month1: while 12-month3<month2: if month3==1 or month3==3 or month3==5 or month3==7 or month3==8 or month3==10 or month3==12: age=age+1 if month3==2: age=age-2 month3=month3+1 # #print '8 ',age return age ## # Your code here. ## print daysBetweenDates(2011,6,30,2012,6,30) # Test routine 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()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question