Python calendarのメソッド一覧|Calendar・TextCalendar・HTMLCalendarの全メソッドと違い
Pythonのcalendarモジュールのメソッドは、日付をデータで取り出すCalendarクラスの14個(itermonthdates()・monthdatescalendar()など)、文字のカレンダーを作るTextCalendarの10個(formatmonth()・prmonth()など)、HTMLを作るHTMLCalendarの4個(formatmonth()・formatyearpage()など)に分かれます。用途で選ぶなら「データで扱う→monthdatescalendar()」「画面に表示→prmonth()」「Webに出す→HTMLCalendar.formatmonth()」です。
この記事は、公式ドキュメント(calendar — General calendar-related functions、Python 3.15.0版、2026年10月18日確認)に載っているメソッドを全部並べ、似た名前のメソッドの違いを実行結果で比べる早見表です。実行結果はPython 3.12で確認し、例には2026年10月を使っています。クラスそのものの使い分けはcalendarのクラスの使い方、カレンダーを表示するだけならPythonでカレンダーを表示・出力する方法のほうが近道です。
calendarのメソッド一覧(クラス別)
Calendarクラス(日付をデータで返す)
| メソッド | 返すもの | 1要素の形 |
|---|---|---|
getfirstweekday() |
週の始まりの曜日番号 | 0(月)〜6(日) |
setfirstweekday(n) |
週の始まりを変える | なし |
iterweekdays() |
1週間分の曜日番号のイテレータ | 0 |
itermonthdates(y, m) |
月の日付のイテレータ(前後の月を含む) | date |
itermonthdays(y, m) |
日の番号のイテレータ | 1(月外は0) |
itermonthdays2(y, m) |
日と曜日のイテレータ | (1, 3) |
itermonthdays3(y, m) |
年月日のイテレータ | (2026, 10, 1) |
itermonthdays4(y, m) |
年月日と曜日のイテレータ | (2026, 10, 1, 3) |
monthdayscalendar(y, m) |
週ごとのリスト | 日の番号7個 |
monthdays2calendar(y, m) |
週ごとのリスト | (日, 曜日)7個 |
monthdatescalendar(y, m) |
週ごとのリスト | date7個 |
yeardayscalendar(y, width=3) |
1年分(月を横width個ずつ) | 日の番号 |
yeardays2calendar(y, width=3) |
1年分 | (日, 曜日) |
yeardatescalendar(y, width=3) |
1年分 | date |
このほか、週の始まりは属性firstweekdayでも読み書きできます。itermonthdays3()・itermonthdays4()はPython 3.7で追加されました。
TextCalendarクラス(文字のカレンダー)
| メソッド | 動き |
|---|---|
formatmonth(y, m, w=0, l=0) |
1か月分を文字列で返す |
prmonth(y, m, w=0, l=0) |
1か月分をその場で表示する(戻り値はNone) |
formatyear(y, w=2, l=1, c=6, m=3) |
1年分を文字列で返す |
pryear(y, ...) |
1年分を表示する |
formatweek(week, width) |
1週間分の行(改行なし) |
prweek(week, width) |
1週間分を表示する |
formatday(day, weekday, width) |
1日分のセル(0なら空白) |
formatweekday(weekday, width) |
曜日名1つ |
formatweekheader(width) |
曜日の見出し行 |
formatmonthname(y, m, width, withyear=True) |
月名の見出し |
HTMLCalendarクラス(HTMLのカレンダー)
| メソッド・属性 | 動き |
|---|---|
formatmonth(y, m, withyear=True) |
1か月分の<table>を文字列で返す |
formatyear(y, width=3) |
1年分の<table>を文字列で返す |
formatyearpage(y, width=3, css='calendar.css', encoding=None) |
1年分の完全なHTMLページをbytesで返す |
formatmonthname(y, m, withyear=True) |
月名の見出し行<tr> |
cssclassesなど |
曜日ごとのCSSクラス名(['mon', 'tue', ..., 'sun']) |
TextCalendarとHTMLCalendarはCalendarを継承しているので、上のCalendarのメソッドもそのまま使えます。HTMLCalendarにも内部でformatday()やformatweek()があり、これを上書きすると見た目を変えられます(後述)。
似たメソッドの違いを実行結果で比べる
itermonthdays・2・3・4とitermonthdatesの違い
どれも「月の日付を1つずつ取り出す」メソッドで、返す形だけが違います。2026年10月1日は木曜日なので、月曜始まりだと最初の3つは9月の日付(または0)になります。
import calendar
c = calendar.Calendar() # 月曜始まり
print(list(c.itermonthdates(2026, 10))[:3])
print(list(c.itermonthdays(2026, 10))[:5])
print(list(c.itermonthdays2(2026, 10))[:5])
print(list(c.itermonthdays3(2026, 10))[:3])
print(list(c.itermonthdays4(2026, 10))[:3])
実行結果:
[datetime.date(2026, 9, 28), datetime.date(2026, 9, 29), datetime.date(2026, 9, 30)]
[0, 0, 0, 1, 2]
[(0, 0), (0, 1), (0, 2), (1, 3), (2, 4)]
[(2026, 9, 28), (2026, 9, 29), (2026, 9, 30)]
[(2026, 9, 28, 0), (2026, 9, 29, 1), (2026, 9, 30, 2)]
ポイントは、itermonthdays()とitermonthdays2()だけが月の外を0で埋めることです。itermonthdates()・itermonthdays3()・itermonthdays4()は前後の月の実際の日付を返すので、月の日だけを処理したいときはd.month == 10のように月で絞り込みます。
monthdayscalendar・monthdays2calendar・monthdatescalendarの違い
こちらは「週ごとのリスト」で返すメソッドです。1週目だけを比べます。
import calendar
c = calendar.Calendar()
print(c.monthdayscalendar(2026, 10)[0])
print(c.monthdays2calendar(2026, 10)[0])
print(c.monthdatescalendar(2026, 10)[0])
実行結果:
[0, 0, 0, 1, 2, 3, 4]
[(0, 0), (0, 1), (0, 2), (1, 3), (2, 4), (3, 5), (4, 6)]
[datetime.date(2026, 9, 28), datetime.date(2026, 9, 29), datetime.date(2026, 9, 30), datetime.date(2026, 10, 1), datetime.date(2026, 10, 2), datetime.date(2026, 10, 3), datetime.date(2026, 10, 4)]
表の形で並べたいならmonthdayscalendar()、日付の計算までしたいならmonthdatescalendar()が便利です。例として「今月の第3金曜日」を求めます。
import calendar
weeks = calendar.Calendar().monthdatescalendar(2026, 10)
fridays = [d for w in weeks for d in w
if d.month == 10 and d.weekday() == calendar.FRIDAY]
print("第3金曜日:", fridays[2])
実行結果:
第3金曜日: 2026-10-16
year系メソッドの入れ子の形
yeardayscalendar(2026, width=3)は「行(3か月ずつ4行)→月→週→日」という4重のリストです。
import calendar
y = calendar.Calendar().yeardayscalendar(2026, width=3)
print(len(y), len(y[0]), len(y[0][0]))
実行結果:
4 3 5
4行、1行に3か月、2026年1月は5週という意味です。
format系とpr系の違い(TextCalendar)
format〜は文字列を返すだけ、pr〜はその場で表示してNoneを返します。ファイルに保存したり加工したりするならformat系です。
import calendar
tc = calendar.TextCalendar(firstweekday=calendar.SUNDAY)
s = tc.formatmonth(2026, 10)
print(type(s).__name__)
r = tc.prmonth(2026, 10)
print(r)
実行結果:
str
October 2026
Su Mo Tu We Th Fr Sa
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
None
部品を作るメソッドも確認しておきます。
import calendar
tc = calendar.TextCalendar(firstweekday=calendar.SUNDAY)
print(repr(tc.formatweekheader(2)))
print(repr(tc.formatweekday(0, 3)))
print(repr(tc.formatweek(tc.monthdays2calendar(2026, 10)[0], 2)))
実行結果:
'Su Mo Tu We Th Fr Sa'
'Mon'
' 1 2 3'
HTMLCalendarのformatmonthとformatyearpageの違い
formatmonth()は<table>部分だけの文字列、formatyearpage()はXML宣言から始まる完全なページをbytesで返します。ファイルに書くときのモードが変わるので注意してください。
import calendar
hc = calendar.HTMLCalendar(firstweekday=calendar.SUNDAY)
print(type(hc.formatmonth(2026, 10)).__name__)
p = hc.formatyearpage(2026)
print(type(p).__name__, p[:38])
with open("year.html", "wb") as f: # bytesなので "wb"
f.write(p)
実行結果:
str
bytes b'<?xml version="1.0" encoding="utf-8"?>'
formatday()を上書きして祝日に印を付ける
HTMLCalendarを継承してformatday()を上書きすると、日ごとにCSSクラスを足せます。formatmonthname()も上書きすれば見出しを日本語にできます。2026年10月12日(スポーツの日、内閣府「国民の祝日について」の祝日CSVで2026年10月18日確認)に印を付けます。
import calendar
class HolidayCalendar(calendar.HTMLCalendar):
def __init__(self, holidays, firstweekday=0):
super().__init__(firstweekday)
self.holidays = holidays
def formatday(self, day, weekday):
if day == 0:
return '<td class="noday"> </td>'
cls = self.cssclasses[weekday]
if day in self.holidays:
cls += " holiday"
return f'<td class="{cls}">{day}</td>'
def formatmonthname(self, theyear, themonth, withyear=True):
s = f"{theyear}年{themonth}月" if withyear else f"{themonth}月"
return f'<tr><th colspan="7" class="month">{s}</th></tr>'
hc = HolidayCalendar({12})
html = hc.formatmonth(2026, 10)
for line in html.splitlines()[1:2] + [l for l in html.splitlines() if "holiday" in l]:
print(line)
実行結果:
<tr><th colspan="7" class="month">2026年10月</th></tr>
<tr><td class="mon holiday">12</td><td class="tue">13</td><td class="wed">14</td><td class="thu">15</td><td class="fri">16</td><td class="sat">17</td><td class="sun">18</td></tr>
あとはCSSで.holiday { color: red; }のように色を付けるだけです。祝日データを内閣府のCSVから読み込む方法はPythonでカレンダーを表示・出力する方法で扱っています。
setfirstweekday():インスタンスとモジュール関数の違い
Calendarクラスのsetfirstweekday()はそのインスタンスだけの設定です。モジュール関数のcalendar.setfirstweekday()(calendar.month()などに効く全体の設定)には影響しません。
import calendar
c = calendar.Calendar()
print(c.getfirstweekday())
c.setfirstweekday(calendar.SUNDAY)
print(c.getfirstweekday(), calendar.firstweekday())
実行結果:
0
6 0
モジュール関数(monthrange()やisleap()など)はcalendar 関数一覧にまとめています。
よくあるエラー
月に1〜12以外を渡すとIllegalMonthErrorになります。Python 3.12からはValueErrorのサブクラスにもなったので、except ValueErrorで受けられます(公式ドキュメントは、新しいコードでIndexErrorで受けるのを避けるよう案内しています)。
import calendar
try:
calendar.Calendar().monthdayscalendar(2026, 13)
except ValueError as e:
print(type(e).__name__, e)
実行結果(Python 3.12):
IllegalMonthError bad month number 13; must be 1-12
やりたいこと別の早見表
| やりたいこと | 使うメソッド |
|---|---|
| 月の日付を1日ずつ処理(日付計算もする) | itermonthdates()/itermonthdays3() |
| 曜日つきで処理(土日の抽出など) | itermonthdays2()/itermonthdays4() |
| 週ごとの表にする | monthdayscalendar() |
| 第n曜日を求める | monthdatescalendar() |
| 文字のカレンダーを文字列で得る/表示する | formatmonth()/prmonth() |
| HTMLのカレンダーを作る | HTMLCalendar.formatmonth() |
| HTMLの1年分のページを保存 | formatyearpage()(bytes) |
| 祝日や予定に印を付ける | formatday()を上書き |
よくある質問
Q. itermonthdates()で前の月の日付まで出てくるのはなぜですか?
週を7日そろえるために、月の前後の日付も返す仕様だからです。その月だけが欲しいときは、d.monthで絞り込むか、月外を0で返すitermonthdays()を使います。
Q. monthdayscalendar()とcalendar.monthcalendar()の違いは?
返す形は同じです。monthdayscalendar()はインスタンスに設定した週の始まりを使い、calendar.monthcalendar()はモジュール全体の設定を使います。
Q. formatmonth()とprmonth()はどう使い分けますか?
formatmonth()は文字列を返すだけなので、保存や加工に使います。prmonth()はその場で表示し、戻り値はNoneです。
Q. formatyearpage()でファイルに書くとエラーになります
formatyearpage()はbytesを返すため、open()を”wb”(バイナリ書き込み)で開いて書き込みます。
あわせて読みたい
- Python calendar クラス一覧|Calendar・TextCalendar・HTMLCalendar
- Python calendarモジュールのメンバー一覧|関数・クラス・定数・例外の早見表 | テックジム
- Python calendar メンバー一覧|関数・クラス・メソッド・定数・属性
- Python標準ライブラリのクラスとは?関数・メソッドとの違いも解説
- Python標準ライブラリのメソッドとは?関数との違いや使い方を解説
Python標準ライブラリの関連記事
calendarは標準ライブラリに含まれるモジュールのひとつです。標準ライブラリ全体の構成や、ほかにどんなモジュールがあるのかは次の記事でまとめています。




