nresni

nresni

Retrieving the first and last day of previous month to build a query with a range date?

Hi,

I try to retrieve the first and last day of previous month to build a query with a range date.

Right now I’m doing this:

def foo(%DateTime{month: month, year: year} = current_date) do

  # eg. 2019-01-01 00:00:00
  start_date = %{current_date | month: month - 1, day: 1, hour: 00, minute: 00, second: 00}

  days_of_month = DateTime.to_date(start_date) |> Date.days_in_month()

  # eg. 2019-01-31 23:59:59
  end_date = DateTime.add(start_date, (days_of_month - 1) * 86_400 + 86_399, :seconds)

  ...
end

For the moment I don’t handle the year change :smile: , but before continuing I wanted to know if you had another way of doing it.

Thanks

Marked As Solved

LostKobrakai

LostKobrakai

Please don’t do such calculations on your own you’re bound to get into trouble with things like daylight-savings time switches or leap-seconds and things like that. Use a library like timex or calendar for calculations on datetimes.

Also Liked

rio517

rio517

Just in case someone stumbles across this, there are built-in functions to help with these calculations.

iex> Date.beginning_of_month(~D[2000-01-31])
~D[2000-01-01]
iex> Date.end_of_month(~D[2000-01-01])
~D[2000-01-31]

https://hexdocs.pm/elixir/Date.html#beginning_of_month/1

nresni

nresni

Oh yes, my bad, timex has everything I need.

Thanks

PJUllrich

PJUllrich

Author of Building Table Views with Phoenix LiveView

Just another note that since Elixir 1.17, you can shift a date using Date.shift/2.

So, to get the beginning and end of the previous month, you could do:

beginning_of_previous_month = 
    Date.utc_today() 
    |> Date.beginning_of_month()
    |> Date.shift(months: -1)

end_of_previous_month = Date.end_of_month(beginning_of_previous_month)

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement