■ 특정일이 속하는 월(Month)의 마지막 날짜를 구하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import datetime def IsLeapYear(year): if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0): return True else: return False MonthDayCountList = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] def GetMonthLastDate(sourceDate): dayCount = MonthDayCountList[sourceDate.month - 1] if sourceDate.month == 2: if IsLeapYear(sourceDate.year): dayCount += 1 targetDate = datetime.datetime(sourceDate.year, sourceDate.month, dayCount) return targetDate if __name__ == "__main__": print(GetMonthLastDate(datetime.datetime(2016, 2 , 16))) # 2016-02-29 00:00:00 print(GetMonthLastDate(datetime.datetime(2019, 11, 16))) # 2019-11-30 00:00:00 |