Edited at

Pythonで翌月初を出力したい

More than 3 years have passed since last update.

検索したら2パターン目のほうばかり引っかかっり、別解もあるよということでメモ。


datetimeとcalendarを使う

import datetime

import calendar

dt = datetime.datetime.today()
max_day = calendar.monthrange(dt.year, dt.month)[1]
nm = datetime.date(dt.year, dt.month, max_day) + datetime.timedelta(days=1)


datetimeとdateutilのrelativedeltaを使う

import datetime

from dateutil.relativedelta import relativedelta

dt = datetime.datetime.today()
nm = dt + relativedelta(months=1)