python week number 와 week number 의 날짜 구하기.
1. week number 구하기.
1 2 3 4 5 6 7 8 |
>>> from datetime import datetime >>> dt = datetime.now() >>> dt datetime.datetime(2013, 2, 25, 14, 49, 53, 870916) >>> weeknumber = dt.isocalendar()[1] >>> weeknumber 9 >>> |
2. week number 의 포함된 날짜들 구하기.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
>>> from datetime import date, timedelta, datetime >>> >>> def weekBoundaries(year, week): ... startOfYear = date(year, 1, 1) ... now = startOfYear + timedelta(weeks=week-1) ... sun = now - timedelta(days=now.isoweekday() % 7) ... weekdays = list() ... weekdays.append(sun.strftime("%Y%m%d")) ... weekdays.append((sun + timedelta(days=1)).strftime("%Y%m%d")) ... weekdays.append((sun + timedelta(days=2)).strftime("%Y%m%d")) ... weekdays.append((sun + timedelta(days=3)).strftime("%Y%m%d")) ... weekdays.append((sun + timedelta(days=4)).strftime("%Y%m%d")) ... weekdays.append((sun + timedelta(days=5)).strftime("%Y%m%d")) ... weekdays.append((sun + timedelta(days=6)).strftime("%Y%m%d")) ... return weekdays ... >>> dt = datetime.now() >>> year = dt.year >>> week = dt.isocalendar()[1] >>> weekdays = weekBoundaries(year, week) >>> weekdays ['20130224', '20130225', '20130226', '20130227', '20130228', '20130301', '20130302'] >>> |
참고 : http://bytes.com/topic/python/answers/499819-getting-start-end-dates-given-week-number