Created
September 25, 2020 11:04
-
-
Save vhf/8ce0f1b10b9f27cf17ccb95197d4c763 to your computer and use it in GitHub Desktop.
Number of weekdays between two dates (elixir)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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