Pythonには標準ライブラリとしてcalendarがあります。
月毎のカレンダーを文字列で表すには次のとおりとします。
import calendar
year = 2021
month = 7
text0 = calendar.month(year, month)
print(text0)
year = 2021
month = 7
text0 = calendar.month(year, month)
print(text0)
デフォルトは月曜日始まりです。
日曜日始まりにするには次のように「setfirstweekday」を使います。
import calendar
year = 2021
month = 7
calendar.setfirstweekday(calendar.SUNDAY)
text0 = calendar.month(year, month)
print(text0)
year = 2021
month = 7
calendar.setfirstweekday(calendar.SUNDAY)
text0 = calendar.month(year, month)
print(text0)
これと同じことをcalendarを使わずに再現してみます。
def calendar_month(year, month, sunday_start = False):
# 月のカレンダーを文字列で返す。
# デフォルトは月曜日始まり
first_date = datetime.date(year, month, 1)
first_yobi = first_date.weekday() # 0:月 6:日
if sunday_start == False:
first_yobi -= 1
if first_yobi == 6:
first_yobi = - 1
if month == 12:
temp_year = year + 1
temp_month = 1
else:
temp_year = year
temp_month = month
last_date = datetime.date(temp_year, temp_month + 1, 1) - datetime.timedelta(days = 1)
last_day = last_date.day
num_day = first_yobi + last_day + 1
first_sunday = first_date - datetime.timedelta(first_yobi)
num_week = math.ceil(num_day / 7)
lines = []
title = first_date.strftime("%B %Y") # 月(英語) 年
lines.append(title.center(20, " ")) # 中央揃え
if sunday_start:
lines.append("Su Mo Tu We Th Fr Sa")
else:
lines.append("Mo Tu We Th Fr Sa Su")
for r in range(num_week):
parts = []
for c in range(7):
dateobj = first_sunday + datetime.timedelta(days = r * 7 + c - 1)
if dateobj.month == month:
this_day = str(dateobj.day).rjust(2)
else:
this_day = " "
parts.append(this_day)
lines.append(" ".join(parts))
text = "\n".join(lines)
return text
# 月のカレンダーを文字列で返す。
# デフォルトは月曜日始まり
first_date = datetime.date(year, month, 1)
first_yobi = first_date.weekday() # 0:月 6:日
if sunday_start == False:
first_yobi -= 1
if first_yobi == 6:
first_yobi = - 1
if month == 12:
temp_year = year + 1
temp_month = 1
else:
temp_year = year
temp_month = month
last_date = datetime.date(temp_year, temp_month + 1, 1) - datetime.timedelta(days = 1)
last_day = last_date.day
num_day = first_yobi + last_day + 1
first_sunday = first_date - datetime.timedelta(first_yobi)
num_week = math.ceil(num_day / 7)
lines = []
title = first_date.strftime("%B %Y") # 月(英語) 年
lines.append(title.center(20, " ")) # 中央揃え
if sunday_start:
lines.append("Su Mo Tu We Th Fr Sa")
else:
lines.append("Mo Tu We Th Fr Sa Su")
for r in range(num_week):
parts = []
for c in range(7):
dateobj = first_sunday + datetime.timedelta(days = r * 7 + c - 1)
if dateobj.month == month:
this_day = str(dateobj.day).rjust(2)
else:
this_day = " "
parts.append(this_day)
lines.append(" ".join(parts))
text = "\n".join(lines)
return text
使い方は次のとおりです。
import datetime
import math
year = 2024
month = 5
# 月曜日始まり
text1 = calendar_month(year, month)
print(text1)
# 日曜日始まり
text1 = calendar_month(year, month, True)
print(text1)
import math
year = 2024
month = 5
# 月曜日始まり
text1 = calendar_month(year, month)
print(text1)
# 日曜日始まり
text1 = calendar_month(year, month, True)
print(text1)
コメント