date - Week Number for Actual Calendar Python -
can tell me how week number in python actual calendar.
ex: 2016-01-01 2016-01-07 = week 1 2016-01-08 2016-01-14 = week 2 i tried 2 ways none of them working
- using
isocalendar()
but not work year end , year start week. ex:
datetime.date(2016,01,01).isocalendar()[1] # should be: 1 # got 53 - is following:
dt = date(2016,01,02) d = strftime("%u", time.strptime(str(dt),"%y-%m-%d")) print d # 00 dt = date(2016,01,03) d = strftime("%u", time.strptime(str(dt),"%y-%m-%d")) print d # 01 both ways not satisfy requirement. there other library can use week number in actual calendar ?
are talking actual number of 7 day periods rather week number you're in? keep in mind in 2016, jan 3rd first day of second week.
if you're looking @ 7 day period, should count days since beginning of year , floor div 7
dt = datetime.datetime(year=2016, month=1, day=2) days = dt - datetime.datetime(year=dt.year, month=1, day=1).days weeks = days // 7 + 1 the 29th should first day of week 5. let's try it.
dt = datetime.datetime(year=2016, month=1, day=29) days = dt - datetime.datetime(year=dt.year, month=1, day=1).days weeks = days // 7 + 1 # 5
Comments
Post a Comment