Calendar v1.0.0 Calendar.Date View Source
The Date module provides a struct to represent a simple date: year, month and day.
Link to this section Summary
Functions
Advances date
by days
number of days.
Like advance/2
, but returns the result directly - not tagged with :ok.
This function might raise an error.
Returns true if the first date is after the second date
Returns true if the first date is before the second date
Like dates_for_week_number/2 but takes a tuple of {year, week_num} instead
Takes a year and an ISO week number and returns a list with the dates in that week.
Day number in year for provided 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.
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 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.
Stream of dates after the date provided as argument.
Get a stream of dates. Takes a starting date and an end date. Includes end date.
Does not include start date unless true
is passed
as the third argument.
Stream of dates before the date provided as argument.
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 unless true
is passed
as the third argument.
Difference in days between two dates.
Returns true
if the date
is a Friday.
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
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.
Examples
iex> from_ordinal(2015, 1)
{:ok, %Date{day: 1, month: 1, year: 2015}}
iex> from_ordinal(2015, 270)
{:ok, %Date{day: 27, month: 9, year: 2015}}
iex> from_ordinal(2015, 999)
{:error, :invalid_ordinal_date}
Examples
iex> from_ordinal!(2015, 1)
%Date{day: 1, month: 1, year: 2015}
iex> from_ordinal!(2015, 270)
%Date{day: 27, month: 9, year: 2015}
iex> from_ordinal!(2015, 365)
%Date{day: 31, month: 12, year: 2015}
Takes a date, a year and an ISO week number and returns true if the date is in the week.
Returns true
if the date
is a Monday.
Takes a Date struct and returns another one representing the next day.
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.
Takes a Date struct and returns another one representing the previous day.
Takes two variables that contain a date.
Returns true
if the date
is a Saturday.
Subtract days
number of days from date.
Returns true
if the date
is a Sunday.
Returns true
if the date
is a Thursday.
Takes a Date struct and returns an erlang style date tuple.
Takes a Date struct and returns the number of gregorian days since year 0.
Returns a string with the date in ISO format.
Returns the date for the time right now in the provided timezone.
Returns the date for the time right now in UTC.
Returns true
if the date
is a Tuesday.
Returns true
if the date
is a Wednesday.
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.
Link to this section Functions
add(date, days) View Source
add!(date, days) View Source
advance(date, days) View Source
Advances date
by days
number of days.
Examples
# Date struct advanced by 3 days
iex> from_erl!({2014,12,27}) |> advance(3)
{:ok, %Date{day: 30, month: 12, year: 2014} }
# Date struct turned back 2 days
iex> from_erl!({2014,12,27}) |> advance(-2)
{:ok, %Date{day: 25, month: 12, year: 2014} }
# Date tuple turned back 2 days
iex> {2014,12,27} |> advance(-2)
{:ok, %Date{day: 25, month: 12, year: 2014} }
# When passing a DateTime, NaiveDateTime or datetime tuple
# the time part is ignored. A Date struct is returned.
iex> {{2014,12,27}, {21,30,59}} |> Calendar.NaiveDateTime.from_erl! |> advance(-2)
{:ok, %Date{day: 25, month: 12, year: 2014} }
iex> {{2014,12,27}, {21,30,59}} |> advance(-2)
{:ok, %Date{day: 25, month: 12, year: 2014} }
advance!(date, days) View Source
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)
%Date{day: 30, month: 12, year: 2014}
iex> {2014,12,27} |> advance!(-2)
%Date{day: 25, month: 12, year: 2014}
after?(first_date_cont, second_date_cont) View Source
Returns true if the first date is after the second date
iex> from_erl!({2014,12,27}) |> after?(from_erl!({2014,12,20}))
true
iex> from_erl!({2014,12,27}) |> after?(from_erl!({2014,12,29}))
false
before?(first_date_cont, second_date_cont) View Source
Returns true if the first date is before the second date
iex> from_erl!({2014,12,27}) |> before?(from_erl!({2014,12,20}))
false
iex> from_erl!({2014,12,27}) |> before?(from_erl!({2014,12,29}))
true
dates_for_week_number(arg) View Source
Like dates_for_week_number/2 but takes a tuple of {year, week_num} instead
dates_for_week_number(year, week_num) View Source
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)
[%Date{day: 29, month: 12, year: 2014}, %Date{day: 30, month: 12, year: 2014},
%Date{day: 31, month: 12, year: 2014}, %Date{day: 1, month: 1, year: 2015},
%Date{day: 2, month: 1, year: 2015}, %Date{day: 3, month: 1, year: 2015},
%Date{day: 4, month: 1, year: 2015}]
iex> dates_for_week_number(2015, 2)
[%Date{day: 5, month: 1, year: 2015}, %Date{day: 6, month: 1, year: 2015},
%Date{day: 7, month: 1, year: 2015}, %Date{day: 8, month: 1, year: 2015},
%Date{day: 9, month: 1, year: 2015}, %Date{day: 10, month: 1, year: 2015},
%Date{day: 11, month: 1, year: 2015}]
iex> dates_for_week_number(2015, 53)
[%Date{day: 28, month: 12, year: 2015}, %Date{day: 29, month: 12, year: 2015},
%Date{day: 30, month: 12, year: 2015}, %Date{day: 31, month: 12, year: 2015},
%Date{day: 1, month: 1, year: 2016}, %Date{day: 2, month: 1, year: 2016},
%Date{day: 3, month: 1, year: 2016}]
day_number_in_year(date) View Source
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) View Source
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) View Source
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) View Source
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) View Source
Stream of dates after the date provided as argument.
iex> days_after({2014,12,27}) |> Enum.take(6)
[%Date{day: 28, month: 12, year: 2014}, %Date{day: 29, month: 12, year: 2014},
%Date{day: 30, month: 12, year: 2014}, %Date{day: 31, month: 12, year: 2014}, %Date{day: 1, month: 1, year: 2015},
%Date{day: 2, month: 1, year: 2015}]
days_after_until(from_date, until_date, include_from_date \\ false) View Source
Get a stream of dates. Takes a starting date and an end date. Includes end date.
Does not include start date unless true
is passed
as the third argument.
iex> days_after_until({2014,12,27}, {2014,12,29}) |> Enum.to_list
[%Date{day: 28, month: 12, year: 2014}, %Date{day: 29, month: 12, year: 2014}]
iex> days_after_until({2014,12,27}, {2014,12,29}, true) |> Enum.to_list
[%Date{day: 27, month: 12, year: 2014}, %Date{day: 28, month: 12, year: 2014}, %Date{day: 29, month: 12, year: 2014}]
days_before(from_date) View Source
Stream of dates before the date provided as argument.
iex> days_before(from_erl!({2014,12,27})) |> Enum.take(3)
[%Date{day: 26, month: 12, year: 2014}, %Date{day: 25, month: 12, year: 2014},
%Date{day: 24, month: 12, year: 2014}]
days_before_until(from_date, until_date, include_from_date \\ false) View Source
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 unless true
is passed
as the third argument.
iex> days_before_until({2014,12,27}, {2014,12,24}) |> Enum.to_list
[%Date{day: 26, month: 12, year: 2014}, %Date{day: 25, month: 12, year: 2014}, %Date{day: 24, month: 12, year: 2014}]
iex> days_before_until({2014,12,27}, {2014,12,24}, false) |> Enum.to_list
[%Date{day: 26, month: 12, year: 2014}, %Date{day: 25, month: 12, year: 2014}, %Date{day: 24, month: 12, year: 2014}]
iex> days_before_until({2014,12,27}, {2014,12,24}, true) |> Enum.to_list
[%Date{day: 27, month: 12, year: 2014}, %Date{day: 26, month: 12, year: 2014}, %Date{day: 25, month: 12, year: 2014}, %Date{day: 24, month: 12, year: 2014}]
diff(first_date_cont, second_date_cont) View Source
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) View Source
Returns true
if the date
is a Friday.
Examples
iex> {2015, 7, 10} |> friday?
true
iex> {2015, 7, 7} |> friday?
false
from_erl(arg) View Source
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, %Date{day: 27, month: 12, year: 2014}}
iex> from_erl({2014,99,99})
{:error, :invalid_date}
from_erl!(erl_date) View Source
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}
%Date{day: 27, month: 12, year: 2014}
from_ordinal(year, ordinal_day) View Source
Examples
iex> from_ordinal(2015, 1)
{:ok, %Date{day: 1, month: 1, year: 2015}}
iex> from_ordinal(2015, 270)
{:ok, %Date{day: 27, month: 9, year: 2015}}
iex> from_ordinal(2015, 999)
{:error, :invalid_ordinal_date}
from_ordinal!(year, ordinal_day) View Source
Examples
iex> from_ordinal!(2015, 1)
%Date{day: 1, month: 1, year: 2015}
iex> from_ordinal!(2015, 270)
%Date{day: 27, month: 9, year: 2015}
iex> from_ordinal!(2015, 365)
%Date{day: 31, month: 12, year: 2015}
in_week?(date, year, week_num) View Source
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) View Source
Returns true
if the date
is a Monday.
Examples
iex> {2015, 7, 6} |> monday?
true
iex> {2015, 7, 7} |> monday?
false
next_day!(date) View Source
Takes a Date struct and returns another one representing the next day.
iex> from_erl!({2014,12,27}) |> next_day!
%Date{day: 28, month: 12, year: 2014}
iex> from_erl!({2014,12,31}) |> next_day!
%Date{day: 1, month: 1, year: 2015}
number_of_days_in_month(date) View Source
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) View Source
Takes a Date struct and returns another one representing the previous day.
iex> from_erl!({2014,12,27}) |> prev_day!
%Date{day: 26, month: 12, year: 2014}
same_date?(first_date_cont, second_date_cont) View Source
Takes two variables that contain a date.
Returns true if the dates are the same.
iex> from_erl!({2014,12,27}) |> same_date?(from_erl!({2014,12,27}))
true
iex> from_erl!({2014,12,27}) |> same_date?({2014,12,27})
true
iex> from_erl!({2014,12,27}) |> same_date?(from_erl!({2014,12,29}))
false
saturday?(date) View Source
Returns true
if the date
is a Saturday.
Examples
iex> {2015, 7, 11} |> saturday?
true
iex> {2015, 7, 7} |> saturday?
false
subtract(date, days) View Source
Subtract days
number of days from date.
Examples
# Date struct turned back 2 days
iex> from_erl!({2014,12,27}) |> subtract(2)
{:ok, %Date{day: 25, month: 12, year: 2014} }
# Date tuple turned back 2 days
iex> {2014,12,27} |> subtract(2)
{:ok, %Date{day: 25, month: 12, year: 2014} }
# When passing a DateTime, Calendar.NaiveDateTime or datetime tuple
# the time part is ignored. A Date struct is returned.
iex> {{2014,12,27}, {21,30,59}} |> Calendar.NaiveDateTime.from_erl! |> subtract(2)
{:ok, %Date{day: 25, month: 12, year: 2014} }
iex> {{2014,12,27}, {21,30,59}} |> subtract(2)
{:ok, %Date{day: 25, month: 12, year: 2014} }
subtract!(date, days) View Source
sunday?(date) View Source
Returns true
if the date
is a Sunday.
Examples
iex> {2015, 7, 12} |> sunday?
true
iex> {2015, 7, 7} |> sunday?
false
thursday?(date) View Source
Returns true
if the date
is a Thursday.
Examples
iex> {2015, 7, 9} |> thursday?
true
iex> {2015, 7, 7} |> thursday?
false
to_erl(date) View Source
Takes a Date struct and returns an erlang style date tuple.
to_gregorian_days(date) View Source
Takes a Date struct and returns the number of gregorian days since year 0.
iex> from_erl!({2014,12,27}) |> to_gregorian_days
735959
to_s(date) View Source
Returns a string with the date in ISO format.
Examples
iex> {2015, 7, 12} |> to_s
"2015-07-12"
iex> {2015, 7, 7} |> to_s
"2015-07-07"
today!(timezone) View Source
Returns the date for the time right now in the provided timezone.
Examples
> today!("America/Montevideo")
%Date{day: 1, month: 3, year: 2016}
> today!("Australia/Sydney")
%Date{day: 2, month: 3, year: 2016}
today_utc() View Source
Returns the date for the time right now in UTC.
Examples
> today_utc
%Date{day: 1, month: 3, year: 2016}
tuesday?(date) View Source
Returns true
if the date
is a Tuesday.
Examples
iex> {2015, 7, 6} |> tuesday?
false
iex> {2015, 7, 7} |> tuesday?
true
wednesday?(date) View Source
Returns true
if the date
is a Wednesday.
Examples
iex> {2015, 7, 8} |> wednesday?
true
iex> {2015, 7, 9} |> wednesday?
false
week_number(date) View Source
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}
iex> from_erl!({2016, 1, 3}) |> week_number
{2015, 53}