Skip to content

Instantly share code, notes, and snippets.

@vhf
Created Sep 25, 2020
Embed
What would you like to do?
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