Skip to content

Instantly share code, notes, and snippets.

@vhf
Created September 25, 2020 11:04
Show Gist options
  • Save vhf/8ce0f1b10b9f27cf17ccb95197d4c763 to your computer and use it in GitHub Desktop.
Save vhf/8ce0f1b10b9f27cf17ccb95197d4c763 to your computer and use it in GitHub Desktop.
Number of weekdays between two dates (elixir)
defmodule Foo do
def bar(first_day, second_day) do
days_between = Date.diff(second_day, first_day) + 1
dow1 = Date.day_of_week(second_day)
dow2 = Date.day_of_week(second_day)
weekend_days =
Kernel.floor((days_between + dow2) / 7 * 2) +
if(dow1 == 7, do: 1, else: 0) - if(dow2 == 6, do: 1, else: 0)
days_between - weekend_days
end
end
d1 = elem(Date.new(2020, 9, 6), 1)
d2 = elem(Date.new(2020, 9, 19), 1)
Foo.bar(d1, d2)
|> IO.inspect(label: "result")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment