Python 例付きカレンダーチュートリアル

カレンダーモジュール Python 日付、月、年に基づいてさまざまなタスクの計算を可能にするカレンダークラスがあります。その上に、TextCalendarクラスとHTMLCalendarクラスがあります。 Python カレンダーを編集し、必要に応じて使用することができます。

何ができるか見てみましょう Python カレンダー。

ステップ1) コードを実行します。

カレンダー Python

  • コード行 # 1: このモジュールのすべてのクラスをインポートする「カレンダーのインポート」から始めます。
  • コード行3: c=calendar.TextCalendar(calendar.SUNDAY) はインタープリタにテキストカレンダーを作成するように指示します。月の開始は日曜日になります。 Pythonカレンダーのフォーマットは、月の日付を変更することで開始できます。
  • コード行 # 4: str= c.formatmonth(2025,1) 2025 年のカレンダー、月 1 – XNUMX 月を作成しています。
  • コード行 # 5: print str は出力を印刷します。

すぐに値を日曜日から木曜日に変更して、出力を確認してみましょう

カレンダー Python

ステップ2) カレンダーを HTML 形式で印刷することもできます。この機能は、開発者がカレンダーの外観と操作性を変更したい場合に役立ちます。

カレンダー Python

ステップ3) ループ c.itermonthday (2025,4) を使用して月の日数を取得すると、その月の合計日数が取得されます。

カレンダー Python

  • 「30 月」などの特定の月の合計日数を取得するコードを実行すると、出力には XNUMX 日が表示されますが、これらの日数とともに最初と最後にいくつかのゼロも表示されます。 。
  • 出力内のゼロは、その曜日が重複する月にあり、その月に属していないことを意味します。
  • これらのゼロが出力に表示されるのは、コード内で日 (木曜日) を指定しているため、関数「c.itermonthdays」を呼び出すと、木曜日から日数のカウントが開始され、木曜日が日付 1 から始まらない可能性があるためです。st 28月はXNUMX日かもしれないth または29th コードを実行すると、28 日から日数がカウントされます。th 1 月とその後 XNUMX 日までの任意の日st XNUMX月の。 これらの日はゼロとしてカウントされ、出力にはこれらのゼロが表示され、月末にも同じことが当てはまります。
  • したがって、日付 ​​1 ~ 30 を除き、前月と後月のすべての日付が出力にゼロとして表示されます。

ステップ4) 月や平日などのデータをローカル システムから取得できます。

カレンダー Python

  • ここの出力は、ローカルシステムから月名を出力したことを示しています。同様に、以下に示すように曜日名を取得することもできます。
  • 出力はローカル システムに依存します。ローカル システムが他の国の場合は、その国のローカル設定に従って出力が得られるとします。 ここでは月単位なので違いはありませんが、週や日になると確実に異なります。

カレンダー Python

ステップ5) 1 年間の特定の日のリストを取得できます。たとえば、最初の日に監査日があります。 Monday 一週間の。最初の日付を知りたい Monday 毎月ごとに。このコードを使用できます

カレンダー Python

  • mycal = Calendar.monthcalendar(2025, month) はその月のカレンダーを作成します
  • 変数week1とweek2をカレンダーの第XNUMX週と第XNUMX週に設定します。
  • 第 1 週に含まれるかどうかを確認する Monday、監査日を設定します
  • それ以外の場合は、監査日を最初に設定します Monday 2週目に
  • 出力には最初の日付が表示されます。 Monday それはその月に該当します。
  • この Cal オブジェクトの長さは、その月が何週間あるかに基づいて、特定の長さになります。私たちの場合、最初は 1 つまたは 2 つになります。 Monday 最も多くの場合は最初の週にありますが、そうでない場合は 2 週目を検討してください。 2週目も考慮する理由を詳しく見てみましょう。
  • ここではカレンダーの定数を使用しています Monday、カレンダー オブジェクトは日曜日を表す定数を提供します。 Monday、火曜日など。これらは以前にも見たことがあります。したがって、第 1 週で、 Monday 定数は 0 ではありません。ゼロは別の月に属する日を意味することに注意してください。したがって、この場合、ゼロであれば、 Monday それは前月のものです。しかし、最初の場合は、 Monday 0でない場合、監査日は0週目になります。XNUMXの場合は、最初の Monday 月の第 1 週ではなく、第 2 週にあるはずです。
  • それで、私は「わかりました。監査日の変数を次のように設定します」と答えます。 Monday 第2週に代表されます。したがって、監査日は、第 1 週または第 2 週のいずれかの日に戻ってきます。

これが完全なコードです

Python 2例

import calendar
# Create a plain text calendar
c = calendar.TextCalendar(calendar.THURSDAY)
str = c.formatmonth(2025, 1, 0, 0)
print str

# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
str = hc.formatmonth(2025, 1)
print str
# loop over the days of a month
# zeroes indicate that the day of the week is in a next month or overlapping month
for i in c.itermonthdays(2025, 4):
    print i

    # The calendar can give info based on local such a names of days and months (full and abbreviated forms)
    for name in calendar.month_name:
        print name
    for day in calendar.day_name:
        print day
    # calculate days based on a rule: For instance an audit day on the second Monday of every month
    # Figure out what days that would be for each month, we can use the script as shown here
    for month in range(1, 13):
		# It retrieves a list of weeks that represent the month
        mycal = calendar.monthcalendar(2025, month)
		# The first MONDAY has to be within the first two weeks
        week1 = mycal[0]
        week2 = mycal[1]
        if week1[calendar.MONDAY] != 0:
            auditday = week1[calendar.MONDAY]
        else:
        # if the first MONDAY isn't in the first week, it must be in the second week
        	auditday = week2[calendar.MONDAY]
print "%10s %2d" % (calendar.month_name[month], auditday)

Python 3例

import calendar
# Create a plain text calendar
c = calendar.TextCalendar(calendar.THURSDAY)
str = c.formatmonth(2025, 1, 0, 0)
print(str)

# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
str = hc.formatmonth(2025, 1)
print(str)
# loop over the days of a month
# zeroes indicate that the day of the week is in a next month or overlapping month
for i in c.itermonthdays(2025, 4):
    print(i)

    # The calendar can give info based on local such a names of days and months (full and abbreviated forms)
    for name in calendar.month_name:
        print(name)
    for day in calendar.day_name:
        print(day)
    # calculate days based on a rule: For instance an audit day on the second Monday of every month
    # Figure out what days that would be for each month, we can use the script as shown here
    for month in range(1, 13):
		# It retrieves a list of weeks that represent the month
        mycal = calendar.monthcalendar(2025, month)
		# The first MONDAY has to be within the first two weeks
        week1 = mycal[0]
        week2 = mycal[1]
        if week1[calendar.MONDAY] != 0:
            auditday = week1[calendar.MONDAY]
        else:
        # if the first MONDAY isn't in the first week, it must be in the second week
        	auditday = week2[calendar.MONDAY]
print("%10s %2d" % (calendar.month_name[month], auditday))

製品概要

  • In Pythonカレンダーは、月の開始日を変更したり、好きなようにフォーマットすることができます。
  • カレンダーをHTML形式で印刷する
  • ローカル システムから月や平日などのデータを取得します
  • XNUMX 年間の特定の日のリストを取得します