0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Python] 曜日を番号で取得する方法

Posted at

以下の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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?