■ 특정일이 속하는 주(Week)의 첫번째 날짜(월요일)를 구하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import datetime def AddDays(sourceDate, count): targetDate = sourceDate + datetime.timedelta(days = count) return targetDate def GetWeekFirstDate(sourceDate): temporaryDate = datetime.datetime(sourceDate.year, sourceDate.month, sourceDate.day) weekDayCount = temporaryDate.weekday() targetDate = AddDays(temporaryDate, -weekDayCount); return targetDate if __name__ == "__main__": print(GetWeekFirstDate(datetime.datetime(2016, 2 , 21))) # 2016-02-15 00:00:00 print(GetWeekFirstDate(datetime.datetime(2016, 2 , 22))) # 2016-02-22 00:00:00 print(GetWeekFirstDate(datetime.datetime(2016, 2 , 23))) # 2016-02-22 00:00:00 print(GetWeekFirstDate(datetime.datetime(2016, 2 , 24))) # 2016-02-22 00:00:00 print(GetWeekFirstDate(datetime.datetime(2016, 2 , 25))) # 2016-02-22 00:00:00 print(GetWeekFirstDate(datetime.datetime(2016, 2 , 26))) # 2016-02-22 00:00:00 print(GetWeekFirstDate(datetime.datetime(2016, 2 , 27))) # 2016-02-22 00:00:00 print(GetWeekFirstDate(datetime.datetime(2016, 2 , 28))) # 2016-02-22 00:00:00 print(GetWeekFirstDate(datetime.datetime(2016, 2 , 29))) # 2016-02-29 00:00:00 print(GetWeekFirstDate(datetime.datetime(2016, 3 , 1 ))) # 2016-02-29 00:00:00 print(GetWeekFirstDate(datetime.datetime(2019, 11, 9 ))) # 2019-11-04 00:00:00 print(GetWeekFirstDate(datetime.datetime(2019, 11, 10))) # 2019-11-04 00:00:00 print(GetWeekFirstDate(datetime.datetime(2019, 11, 11))) # 2019-11-11 00:00:00 print(GetWeekFirstDate(datetime.datetime(2019, 11, 12))) # 2019-11-11 00:00:00 print(GetWeekFirstDate(datetime.datetime(2019, 11, 13))) # 2019-11-11 00:00:00 print(GetWeekFirstDate(datetime.datetime(2019, 11, 14))) # 2019-11-11 00:00:00 print(GetWeekFirstDate(datetime.datetime(2019, 11, 15))) # 2019-11-11 00:00:00 print(GetWeekFirstDate(datetime.datetime(2019, 11, 16))) # 2019-11-11 00:00:00 print(GetWeekFirstDate(datetime.datetime(2019, 11, 17))) # 2019-11-11 00:00:00 print(GetWeekFirstDate(datetime.datetime(2019, 11, 18))) # 2019-11-18 00:00:00 |