Skip to content

Instantly share code, notes, and snippets.

@ashitaka1963
Last active April 13, 2020 13:04
Show Gist options
  • Save ashitaka1963/810391fe224c580edb22ed5424de2107 to your computer and use it in GitHub Desktop.
Save ashitaka1963/810391fe224c580edb22ed5424de2107 to your computer and use it in GitHub Desktop.
来週の指定した曜日の日付を取得
import datetime
def get_next_target_date(date, target_week):
week = ['月','火','水','木','金','土','日']
# 曜日を数値型で取得
weekday = date.weekday()
# dateから指定した曜日までの加算日数を計算
add_days = 7 - weekday + week.index(target_week)
# dateに加算
next_target_date = date + datetime.timedelta(days = add_days)
return next_target_date
# 現在の日付を取得する。
today = datetime.date.today()
print(today) # 2020-04-13
# 指定した曜日の来週の日付を取得する。
next_date = get_next_target_date(today, '水')
print(next_date) # 2020-04-21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment