■ 특정일이 속하는 주(Week)의 첫번째 날짜(월요일) 구하기

------------------------------------------------------------------------------------------------------------------------

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

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요