I have a start date and an end date and I'd like to know how many months that occurrence fell in. For example if my start date was 2009, 01, 31 and the end date was 2009, 02, 01 I'd like to calculate that the occurrence fell in 2 different months. I've been playing around with datetime and dateutil but I can't seem to come up with anything. Could anyone point me in the right direction?

I think this pretty well does the trick. Anyone spot any pitfalls?

import datetime

startdate = datetime.date(2008,01,01)
enddate = datetime.date(2009,01,02)

y = (enddate.year - startdate.year) * 12
m = ((enddate.month - startdate.month) + y) + 1

print "Months =", str(m)

i think this works fine and i tried it removing str before (m) and it worked to so i can say its good do you have any question?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.