Calendar.Date

The Date module provides a struct to represent a simple date: year, month and day.

Summary

advance!(date, days)

Like advance/2, but returns the result directly - not tagged with :ok. This function might raise an error

advance(date, days)

Advances date by days number of days

days_after(from_date)

Stream of dates after the date provided as argument

days_after_until(from_date, until_date)

Get a stream of dates. Takes a starting date and an end date. Includes end date. Does not include start date

days_before(from_date)

Stream of dates before the date provided as argument

days_before_until(from_date, until_date)

Get a stream of dates going back in time. Takes a starting date and an end date. Includes end date. End date should be before start date. Does not include start date

diff(first_date, second_date)

Difference in days between two dates

from_erl!(erl_date)

Like from_erl without the exclamation point, but does not return a tuple with a tag. Instead returns just a Date if valid. Or raises an exception if the provided date is invalid

from_erl(arg1)

Takes a erlang style date tuple and returns a tuple with an :ok tag and a Date struct. If the provided date is invalid, it will not be tagged with :ok though as shown below:

next_day!(date)

Takes a Date struct and returns another one representing the next day

number_of_days_in_month(date)

Takes a Date struct and returns the number of days in the month of that date. The day of the date provided does not matter - the result is based on the month and the year

prev_day!(date)

Takes a Date struct and returns another one representing the previous day

strftime!(date, string, lang \\ :en)

Format date as string

to_erl(date)

Takes a Date struct and returns an erlang style date tuple

to_gregorian_days(date)

Takes a Date struct and returns the number of gregorian days since year 0

week_number(date)

Takes a Date struct and returns a tuple with the ISO week number and the year that the week belongs to. Note that the year returned does not always match the year provided

Functions

advance(date, days)

Advances date by days number of days.

Examples

iex> from_erl!({2014,12,27}) |> advance(3)
{:ok, %Calendar.Date{day: 30, month: 12, year: 2014} }
iex> from_erl!({2014,12,27}) |> advance(-2)
{:ok, %Calendar.Date{day: 25, month: 12, year: 2014} }
advance!(date, days)

Like advance/2, but returns the result directly - not tagged with :ok. This function might raise an error.

Examples

iex> from_erl!({2014,12,27}) |> advance!(3)
%Calendar.Date{day: 30, month: 12, year: 2014}
days_after(from_date)

Stream of dates after the date provided as argument.

iex> days_after(from_erl!({2014,12,27})) |> Enum.take(6)
[%Calendar.Date{day: 28, month: 12, year: 2014}, %Calendar.Date{day: 29, month: 12, year: 2014},
      %Calendar.Date{day: 30, month: 12, year: 2014}, %Calendar.Date{day: 31, month: 12, year: 2014}, %Calendar.Date{day: 1, month: 1, year: 2015},
      %Calendar.Date{day: 2, month: 1, year: 2015}]
days_after_until(from_date, until_date)

Get a stream of dates. Takes a starting date and an end date. Includes end date. Does not include start date.

iex> days_after_until(from_erl!({2014,12,27}), from_erl!({2014,12,29})) |> Enum.to_list
[%Calendar.Date{day: 28, month: 12, year: 2014}, %Calendar.Date{day: 29, month: 12, year: 2014}]
days_before(from_date)

Stream of dates before the date provided as argument.

iex> days_before(from_erl!({2014,12,27})) |> Enum.take(3)
[%Calendar.Date{day: 26, month: 12, year: 2014}, %Calendar.Date{day: 25, month: 12, year: 2014},
      %Calendar.Date{day: 24, month: 12, year: 2014}]
days_before_until(from_date, until_date)

Get a stream of dates going back in time. Takes a starting date and an end date. Includes end date. End date should be before start date. Does not include start date.

iex> days_before_until(from_erl!({2014,12,27}), from_erl!({2014,12,24})) |> Enum.to_list
[%Calendar.Date{day: 26, month: 12, year: 2014}, %Calendar.Date{day: 25, month: 12, year: 2014}, %Calendar.Date{day: 24, month: 12, year: 2014}]
diff(first_date, second_date)

Difference in days between two dates.

Takes two Date structs: first_date and second_date. Subtracts second_date from first_date.

iex> from_erl!({2014,12,27}) |> diff from_erl!({2014,12,20})
7
iex> from_erl!({2014,12,27}) |> diff from_erl!({2014,12,29})
-2
from_erl(arg1)

Takes a erlang style date tuple and returns a tuple with an :ok tag and a Date struct. If the provided date is invalid, it will not be tagged with :ok though as shown below:

iex> from_erl({2014,12,27})
{:ok, %Calendar.Date{day: 27, month: 12, year: 2014}}

iex> from_erl({2014,99,99})
{:error, :invalid_date}
from_erl!(erl_date)

Like from_erl without the exclamation point, but does not return a tuple with a tag. Instead returns just a Date if valid. Or raises an exception if the provided date is invalid.

iex> from_erl! {2014,12,27}
%Calendar.Date{day: 27, month: 12, year: 2014}
next_day!(date)

Takes a Date struct and returns another one representing the next day.

iex> from_erl!({2014,12,27}) |> next_day!
%Calendar.Date{day: 28, month: 12, year: 2014}
iex> from_erl!({2014,12,31}) |> next_day!
%Calendar.Date{day: 1, month: 1, year: 2015}
number_of_days_in_month(date)

Takes a Date struct and returns the number of days in the month of that date. The day of the date provided does not matter - the result is based on the month and the year.

iex> from_erl!({2014,12,27}) |> number_of_days_in_month
31
iex> from_erl!({2015,2,27}) |> number_of_days_in_month
28
iex> from_erl!({2012,2,27}) |> number_of_days_in_month
29
prev_day!(date)

Takes a Date struct and returns another one representing the previous day.

iex> from_erl!({2014,12,27}) |> prev_day!
%Calendar.Date{day: 26, month: 12, year: 2014}
strftime!(date, string, lang \\ :en)

Format date as string.

Takes

  • date - a Date struct
  • string - formatting string
  • lang (optional) - language code

Examples

iex> strftime!(from_erl!({2014,12,27}), "%Y-%m-%d")
"2014-12-27"
to_erl(date)

Takes a Date struct and returns an erlang style date tuple.

to_gregorian_days(date)

Takes a Date struct and returns the number of gregorian days since year 0.

iex> from_erl!({2014,12,27}) |> to_gregorian_days
735959
week_number(date)

Takes a Date struct and returns a tuple with the ISO week number and the year that the week belongs to. Note that the year returned does not always match the year provided.

iex> from_erl!({2014,12,31}) |> week_number
{2015, 1}
iex> from_erl!({2014,12,27}) |> week_number
{2014, 52}