Issue
I'm currently working on this problem of counting how many days between two dates including leap years.
However it keeps skipping the loop from the beginning, even though the two months and days aren't the same?
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
day_count = 0
while (year2 != year1) and (month2 != month1) and (day2! = day 1):
# print "in loop" // tester
if (day2 != 0):
day2 = day2 - 1
day_count = day_count + 1
else:
if(month2 != 0):
month2 = month2 - 1
if month2 == (9 or 4 or 6 or 11):
day2 = 30
if month2 == 2:
day2 = 28
if (month2 == 2) and (year2 % 4):
day2 = 29
else:
day2 == 31
else:
year2 = year2 - 1
month2 = 12
#print day_count //tester
return day_count
# 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()
Solution
Here's one way.
>>> from datetime import datetime
>>> diff = datetime(2012, 2, 28)-datetime(2012, 1, 1)
>>> diff.days
58
Answered By - Bill Bell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.