---------------------------------------------------
from datetime import date
def after_month(given_date, month):
yyyy = int(((given_date.year * 12 + given_date.month) + month)/12)
mm = int(((given_date.year * 12 + given_date.month) + month)%12)
if mm == 0:
yyyy -= 1
mm = 12
return given_date.replace(year=yyyy, month=mm)
print(next_date, end = ' ')
-----------------------------------------------------
링크에서 처럼 현재를 입력하고 한달후를 출력하고 싶으면 위코드에 무엇을 추가하고 수정해줘야 할까요?
def after_month(given_date, month):
yyyy = int(((given_date.year * 12 + given_date.month) + month)/12)
mm = int(((given_date.year * 12 + given_date.month) + month)%12)
if mm == 0:
yyyy -= 1
mm = 12
return given_date.replace(year=yyyy, month=mm)
print(next_date, end = ' ')
À§ Äڵ带 ¼öÁ¤Çؼ °ªÀ» ³ªÅ¸³»·Á°í Çϴµ¥, ÁÁÀº ¹æ¹ýÀÌ ÀÖÀ¸½Å°¡¿ä?
¿©±â¼ µÎ³ú ±â¸§Ä¥ ÄûÁî.
1¿ù 31ÀÏÀÇ ÇÑ ´Þ ÈÄ´Â ¸î ÀÏ Àϱî¿ä?
ÇÑ ´ÞÀÇ °³³äÀ» 30ÀÏ °ú °°ÀÌ ³¯Â¥ °³³äÀ¸·Î Àâ´Â´Ù¸é ´Þ ´ÜÀ§°¡ ¾Æ´Ñ ³¯Â¥ ´ÜÀ§ÀÇ ¿¬»êÀ» ÇÏ´Â°Ô ¸Â°í
´ÞÀÇ ¸íĪÀ» °¡Áö°í °è»êÇÑ´Ù¸é ÇØ´ç ´Þ¿¡ Ç¥ÇöµÉ ¼ö ¾ø´Â ³¯Â¥¿¡ ´ëÇÑ ¿¹¿Ü 󸮸¦ µÎ´Â °ÍÀÌ ¸ÂÁÒ.
import datetime
from datetime import date
def after_month(given_date, month):
yyyy = int(((given_date.year * 12 + given_date.month) + month)/12)
mm = int(((given_date.year * 12 + given_date.month) + month)%12)
if mm == 0:
yyyy -= 1
mm = 12
return given_date.replace(year=yyyy, month=mm)
today = datetime.datetime.today()
print("today:", today)
print("after 1 month:", after_month(today, 1))
===
°á°ú´Â ¾Æ·¡¿Í °°³×¿ä.
today: 2021-04-21 22:23:12.109035
after 1 month: 2021-05-21 22:23:12.109035
class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
datetime.datetime(2021, 04, 27) °°Àº ½ÄÀ¸·Î ¸¸µé¸é µÇ°Ú±º¿ä.
À§ÀÇ Äڵ忡
another_day = datetime.datetime(1999, 8, 18)
print("another_day:", another_day)
print("after 3 months:", after_month(another_day, 3))
ÀÌ·¸°Ô Ãß°¡Çϸé, °á°ú°¡ ¾Æ·¡Ã³·³ ³ª¿À³×¿ä.
~/workspace$ python3 next_month.py
today: 2021-04-27 01:48:29.779192
after 1 month: 2021-05-27 01:48:29.779192
another_day: 1999-08-18 00:00:00
after 3 months: 1999-11-18 00:00:00
28ÀÏÀÎÁö, 30ÀÏÀÎÁö, ´Þ·Â»ó 1´Þ¸¸ À̵¿ÇÒ °ÍÀÎÁö µî¿¡ ´ëÇؼ¿ä.