Kalends.Date

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

Summary

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

stream(from_date)
stream(from_date, until_date)

Get a stream of dates. Takes a starting date and an optional end date. Includes both start and end date

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

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, %Kalends.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}
%Kalends.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
%Kalends.Date{day: 28, month: 12, year: 2014}
iex> from_erl!({2014,12,31}) |> next_day
%Kalends.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
%Kalends.Date{day: 26, month: 12, year: 2014}
stream(from_date)
stream(from_date, until_date)

Get a stream of dates. Takes a starting date and an optional end date. Includes both start and end date.

iex> stream(from_erl!({2014,12,27}), from_erl!({2014,12,29})) |> Enum.to_list
[%Kalends.Date{day: 27, month: 12, year: 2014}, %Kalends.Date{day: 28, month: 12, year: 2014}, %Kalends.Date{day: 29, month: 12, year: 2014}]
iex> stream(from_erl!({2014,12,27})) |> Enum.take(7)
[%Kalends.Date{day: 27, month: 12, year: 2014}, %Kalends.Date{day: 28, month: 12, year: 2014}, %Kalends.Date{day: 29, month: 12, year: 2014},
      %Kalends.Date{day: 30, month: 12, year: 2014}, %Kalends.Date{day: 31, month: 12, year: 2014}, %Kalends.Date{day: 1, month: 1, year: 2015},
      %Kalends.Date{day: 2, month: 1, year: 2015}]
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}