Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like get DaysInMonth for in elixir?

I have searched on Ecto.DateTime, but unfortunately didn't find anything useful. Is there a way to get last day of the month?

I have a query where I load orders from psql, from yesterday (31 July) to today (1 Aug), with today I just use 1, but for the yesterday it can be 31, 30, 29 or 28. Is there a way to know how many days are in the previous month?

Thanks in advance, and any advise appreciated!
like image 512
Ilya Avatar asked Oct 27 '25 09:10

Ilya


2 Answers

There's :calendar.last_day_of_the_month/2 which accepts a year and month as arguments.

iex(1)> :calendar.last_day_of_the_month(2000, 2)
29
iex(2)> :calendar.last_day_of_the_month(2001, 2)
28

last_day_of_the_month(Year, Month) -> LastDay

Types:

Year = year()
Month = month()
LastDay = ldom()

Computes the number of days in a month.

like image 58
Dogbert Avatar answered Oct 29 '25 22:10

Dogbert


You can use the days_in_month/2 function in the Calendar.ISO implementation in Elixir:

Calendar.ISO.days_in_month(year, month)

For example, Calendar.ISO.days_in_month(2019, 7) will return 31.

You can find more information in the docs here: https://hexdocs.pm/elixir/Calendar.ISO.html#days_in_month/2

like image 28
Anirudh Avatar answered Oct 29 '25 23:10

Anirudh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!