ありがとうございます。
あなたは(1)月曜日のDAY_OF_THE_WEEKに対して日付のDAY_OF_THE_WEEKを比較し、減算することができます
その日から何日:
def monday_in_the_week_of(date) do
from_monday = :calendar.day_of_the_week(date) - 1
date
|> :calendar.date_to_gregorian_days
|> Kernel.-(from_monday)
|> :calendar.gregorian_days_to_date
end
デモ:
defmodule A do
def monday_in_the_week_of(date) do
from_monday = :calendar.day_of_the_week(date) - 1
date
|> :calendar.date_to_gregorian_days
|> Kernel.-(from_monday)
|> :calendar.gregorian_days_to_date
end
end
for d <- 1..15 do
date = {2017, 2, d}
IO.inspect {date, A.monday_in_the_week_of(date)}
end
出力:
{{2017, 2, 1}, {2017, 1, 30}}
{{2017, 2, 2}, {2017, 1, 30}}
{{2017, 2, 3}, {2017, 1, 30}}
{{2017, 2, 4}, {2017, 1, 30}}
{{2017, 2, 5}, {2017, 1, 30}}
{{2017, 2, 6}, {2017, 2, 6}}
{{2017, 2, 7}, {2017, 2, 6}}
{{2017, 2, 8}, {2017, 2, 6}}
{{2017, 2, 9}, {2017, 2, 6}}
{{2017, 2, 10}, {2017, 2, 6}}
{{2017, 2, 11}, {2017, 2, 6}}
{{2017, 2, 12}, {2017, 2, 6}}
{{2017, 2, 13}, {2017, 2, 13}}
{{2017, 2, 14}, {2017, 2, 13}}
{{2017, 2, 15}, {2017, 2, 13}}
ちょっと@Dogbert、あなたのソリューションのおかげで、すべてが動作しないw! – Ilya