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

dates_for_week_number(arg1)

Like dates_for_week_number/2 but takes a tuple of {year, week_num} instead

dates_for_week_number(year, week_num)

Takes a year and an ISO week number and returns a list with the dates in that week

day_number_in_year(date)

Day number in year for provided date

day_of_week(date)

Day of the week as an integer. Monday is 1, Tuesday is 2 and so on. ISO-8601. Sunday is 7. Results can be between 1 and 7

day_of_week_name(date, lang \\ :en)

The name of the day of the week as a string. Takes a language code as the second argument. Defaults to :en for English

day_of_week_zb(date)

Day of the week as an integer with Sunday being 0. Monday is 1, Tuesday is 2 and so on. Results can be between 0 and 6

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_cont, second_date_cont)

Difference in days between two dates

friday?(date)

Returns true if the date is a Friday

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:

in_week?(date, year, week_num)

Takes a date, a year and an ISO week number and returns true if the date is in the week

monday?(date)

Returns true if the date is a Monday

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

saturday?(date)

Returns true if the date is a Saturday

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

DEPRICATED. Use Calendar.Strftime.strftime!/3 instead - it works the same way

sunday?(date)

Returns true if the date is a Sunday

thursday?(date)

Returns true if the date is a Thursday

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

tuesday?(date)

Returns true if the date is a Tuesday

wednesday?(date)

Returns true if the date is a Wednesday

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 is not always the same as the year provided as an argument

Functions

advance(date, days)

Advances date by days number of days.

Examples

# Date struct advanced by 3 days
iex> from_erl!({2014,12,27}) |> advance(3)
{:ok, %Calendar.Date{day: 30, month: 12, year: 2014} }
# Date struct turned back 2 days
iex> from_erl!({2014,12,27}) |> advance(-2)
{:ok, %Calendar.Date{day: 25, month: 12, year: 2014} }
# Date tuple turned back 2 days
iex> {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}
iex> {2014,12,27} |> advance!(-2)
%Calendar.Date{day: 25, month: 12, year: 2014}
dates_for_week_number(arg1)

Like dates_for_week_number/2 but takes a tuple of {year, week_num} instead

dates_for_week_number(year, week_num)

Takes a year and an ISO week number and returns a list with the dates in that week.

iex> dates_for_week_number(2015, 1)
[%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}, %Calendar.Date{day: 3, month: 1, year: 2015},
      %Calendar.Date{day: 4, month: 1, year: 2015}]
iex> dates_for_week_number(2015, 2)
[%Calendar.Date{day: 5, month: 1, year: 2015}, %Calendar.Date{day: 6, month: 1, year: 2015},
      %Calendar.Date{day: 7, month: 1, year: 2015}, %Calendar.Date{day: 8, month: 1, year: 2015},
      %Calendar.Date{day: 9, month: 1, year: 2015}, %Calendar.Date{day: 10, month: 1, year: 2015},
      %Calendar.Date{day: 11, month: 1, year: 2015}]
day_number_in_year(date)

Day number in year for provided date.

Examples

iex> {2015, 1, 1} |> day_number_in_year
1
iex> {2015, 2, 1} |> day_number_in_year
32
# 2015 has 365 days
iex> {2015, 12, 31} |> day_number_in_year
365
# 2000 was leap year and had 366 days
iex> {2000, 12, 31} |> day_number_in_year
366
day_of_week(date)

Day of the week as an integer. Monday is 1, Tuesday is 2 and so on. ISO-8601. Sunday is 7. Results can be between 1 and 7.

See also day_of_week_zb/1

Examples

iex> {2015, 7, 6} |> day_of_week # Monday
1
iex> {2015, 7, 7} |> day_of_week # Tuesday
2
iex> {2015, 7, 5} |> day_of_week # Sunday
7
day_of_week_name(date, lang \\ :en)

The name of the day of the week as a string. Takes a language code as the second argument. Defaults to :en for English.

Examples

iex> {2015, 7, 6} |> day_of_week_name # Monday
"Monday"
iex> {2015, 7, 7} |> day_of_week_name # Tuesday
"Tuesday"
iex> {2015, 7, 5} |> day_of_week_name # Sunday
"Sunday"
day_of_week_zb(date)

Day of the week as an integer with Sunday being 0. Monday is 1, Tuesday is 2 and so on. Results can be between 0 and 6.

Examples

iex> {2015, 7, 5} |> day_of_week_zb # Sunday
0
iex> {2015, 7, 6} |> day_of_week_zb # Monday
1
iex> {2015, 7, 7} |> day_of_week_zb # Tuesday
2
days_after(from_date)

Stream of dates after the date provided as argument.

iex> days_after({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_cont, second_date_cont)

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
friday?(date)

Returns true if the date is a Friday.

Examples

iex> {2015, 7, 10} |> friday?
true
iex> {2015, 7, 7} |> friday?
false
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}
in_week?(date, year, week_num)

Takes a date, a year and an ISO week number and returns true if the date is in the week.

iex> {2015, 1, 1} |> in_week? 2015, 1
true
iex> {2015, 5, 5} |> in_week? 2015, 1
false
monday?(date)

Returns true if the date is a Monday.

Examples

iex> {2015, 7, 6} |> monday?
true
iex> {2015, 7, 7} |> monday?
false
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}
saturday?(date)

Returns true if the date is a Saturday.

Examples

iex> {2015, 7, 11} |> saturday?
true
iex> {2015, 7, 7} |> saturday?
false
strftime!(date, string, lang \\ :en)

DEPRICATED. Use Calendar.Strftime.strftime!/3 instead - it works the same way.

sunday?(date)

Returns true if the date is a Sunday.

Examples

iex> {2015, 7, 12} |> sunday?
true
iex> {2015, 7, 7} |> sunday?
false
thursday?(date)

Returns true if the date is a Thursday.

Examples

iex> {2015, 7, 9} |> thursday?
true
iex> {2015, 7, 7} |> thursday?
false
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
tuesday?(date)

Returns true if the date is a Tuesday.

Examples

iex> {2015, 7, 6} |> tuesday?
false
iex> {2015, 7, 7} |> tuesday?
true
wednesday?(date)

Returns true if the date is a Wednesday.

Examples

iex> {2015, 7, 8} |> wednesday?
true
iex> {2015, 7, 9} |> wednesday?
false
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 is not always the same as the year provided as an argument.

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