If you wanted to measure the number of full weeks between two dates, you could accomplish this with datetime.strptime
and timedelta
like so:
from datetime import datetime, date, timedelta
dateformat = "%Y-%m-%d"
date1 = datetime.strptime("2015-07-09", dateformat)
date2 = datetime.strptime("2016-08-20", dateformat)
weeks = int((date2-date1).days/7)
print weeks
This outputs 58
. The divide by 7 causes the number of weeks to be returned. The number of whole weeks is used (rather than partial) because of int
which returns only the integer portion. If you wanted to get the number of partial weeks, you could divide by 7.0 instead of 7, and ensure that you remove the int
piece.
2
solved Python get number of the week by month