adamu

adamu

Calculating the beginning of next month

I’m having a discussion with a colleague about how to calculate the beginning of the next month given a certain Date using the core library. We have come up with two solutions, but cannot agree on which one is better. Are there any technical reasons to prefer one over the other here? Or better options (without using external libraries)?

Version that relies on using the calendar to calculate the number of days to the next month:

def beginning_of_next_month(%Date{} = date) do
  days_in_month = Date.days_in_month(date)

  date
  |> Date.beginning_of_month()
  |> Date.add(days_in_month)
end

Version that increments the month and relies on pattern matching to handle rolling over the year:

def beginning_of_next_month(%Date{year: year, month: 12}),
  do: Date.new!(year + 1, 1, 1)

def beginning_of_next_month(%Date{year: year, month: month}),
  do: Date.new!(year, month + 1, 1)
  • Date.days_in_month/1 + Date.add/2
  • Pattern match + Date.new/3

0 voters

Marked As Solved

trisolaran

trisolaran

Personally, I’d go for:

date
|> Date.end_of_month()
|> Date.add(1)
11
Post #5

Also Liked

eksperimental

eksperimental

Even simpler:
~D[2000-01-01] |> Date.end_of_month() |> Date.add(1)

c4710n

c4710n

I prefer version 1 - it uses the public API of Date. It is stable. If it is depracated, compiler will warn you about that.

version 2 relies on the structure of Date struct. Generally, I think it is the moving part, and will change potentially in the future. (although the probability is very low)

eksperimental

eksperimental

Funny fact, we wrote the same solution.

adamu

adamu

Perfect. I completely missed Date.end_of_month :man_facepalming:

qhwa

qhwa

I like the increment version but I think it’s incorrect because the day should be always 1.

I’ll probably write:

def beginning_of_next_month(%Date{year: year, month: 12}),
  do: %{date | year: year + 1, month: 1, day: 1}

def beginning_of_next_month(%Date{month: month}),
  do: %{date | month: month + 1, day: 1}

Simple math seems clearer than public API combos to me.
Anyway, it feels good to have Date.beginning_of_next_month/1 out of the box.

Where Next?

Popular in Questions Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

Other popular topics Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement