Month v2.0.0 Month View Source

A data structure and a set of methods for those who work exclusively with months and month ranges.

Please check the documentation for Month as well as Month.Period and Month.Range, which cover some extra use cases.

Using the ~M sigil

To use the ~M sigil, please import Month.Sigils like so:

defmodule SomeModule do
  import Month.Sigils
  # ...
end

Then you can do

date = ~M[2000-01]

Examples

iex> ~M[2019-03].month
3

iex> ~M[2019-03].year
2019

iex> range = Month.Range.new!(~M[2019-01], ~M[2019-03])
#Month.Range<~M[2019-01], ~M[2019-03]>

iex> range.months
[~M[2019-01], ~M[2019-02], ~M[2019-03]]

Link to this section Summary

Functions

Adds or subtracts months from given month

Same as add/2 but returns result or throws

Compares two months and returns if first one is greater (after), equal or less (before) the second one

Returns list of dates in a month

Creates a new Month struct, using either year/month or another struct that has year and month fields, such as Date or DateTime

Sames as new/2 but returns result or throws

Subtracts the given positive number of months from the month

Same as subtract/2 but either returns result or throws

Link to this section Types

Link to this type

t() View Source
t() :: %Month{
  first_date: Date.t(),
  last_date: Date.t(),
  month: integer(),
  year: integer()
}

Link to this section Functions

Link to this function

add(month, num_months) View Source
add(Month.t(), integer()) :: {:ok, Month.t()} | {:error, String.t()}

Adds or subtracts months from given month.

You can pass a negative number of months to subtract.

Examples

iex> {:ok, month} = Month.new(2019, 3)
{:ok, ~M[2019-03]}
iex> Month.add(month, 3)
{:ok, ~M[2019-06]}
Link to this function

add!(month, num_months) View Source
add!(Month.t(), integer()) :: Month.t()

Same as add/2 but returns result or throws.

Link to this function

compare(a, b) View Source
compare(Month.t(), Month.t()) :: :eq | :lt | :gt

Compares two months and returns if first one is greater (after), equal or less (before) the second one.

Examples

iex> Month.compare(~M[2020-03], ~M[2019-12])
:gt
Link to this function

dates(date) View Source
dates(Date.t()) :: [Date.t()]
dates(Month.t()) :: [Date.t()]

Returns list of dates in a month.

Link to this function

new(map) View Source
new(map()) :: {:ok, Month.t()} | {:error, String.t()}

Creates a new Month struct, using either year/month or another struct that has year and month fields, such as Date or DateTime.

Link to this function

new(year, month) View Source
new(integer(), integer()) :: {:ok, Month.t()} | {:error, String.t()}

Sames as new/2 but returns result or throws.

Link to this function

subtract(month, num_months) View Source

Subtracts the given positive number of months from the month.

Same as add/2 when you give it a negative number of months.

Link to this function

subtract!(month, num_months) View Source
subtract!(Month.t(), integer()) :: Month.t()

Same as subtract/2 but either returns result or throws.