以下の3つの取得が可能
関数 | |
---|---|
月曜0始まり 日曜6終わり | weekday() |
月曜1始まり 日曜7終わり | isoweekday() strftime('%u') |
日曜0始まり 土曜6終わり | strftime('%w') |
ただしstrftimeは文字列のため数値として使う場合は変換が必要
import datetime
week = [datetime.datetime.strptime(f'2023/7/{i+10} 12:00:00', '%Y/%m/%d %H:%M:%S') for i in range(7)]
for day in week:
youbi_0 = day.strftime('%a')
youbi_1 = day.weekday()
youbi_2 = day.isoweekday()
youbi_3 = int(day.strftime('%w'))
youbi_4 = int(day.strftime('%u'))
print(f"{youbi_0}: weekday= {youbi_1}, isoweekday= {youbi_2}, strftime_%w= {youbi_3}, strftimee_%u= {youbi_4}")
出力結果
Mon: weekday= 0, isoweekday= 1, strftime_%w= 1, strftimee_%u= 1
Tue: weekday= 1, isoweekday= 2, strftime_%w= 2, strftimee_%u= 2
Wed: weekday= 2, isoweekday= 3, strftime_%w= 3, strftimee_%u= 3
Thu: weekday= 3, isoweekday= 4, strftime_%w= 4, strftimee_%u= 4
Fri: weekday= 4, isoweekday= 5, strftime_%w= 5, strftimee_%u= 5
Sat: weekday= 5, isoweekday= 6, strftime_%w= 6, strftimee_%u= 6
Sun: weekday= 6, isoweekday= 7, strftime_%w= 0, strftimee_%u= 7