Calendar v0.17.1 Calendar.Date
The Date module provides a struct to represent a simple date: year, month and day.
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
Examples
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
Functions
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} }
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}
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
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
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.
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 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 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
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 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
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}]
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}]
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}]
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}]
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
Returns true
if the date
is a Friday.
Examples
iex> {2015, 7, 10} |> friday?
true
iex> {2015, 7, 7} |> friday?
false
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}
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}
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.
iex> {2015, 1, 1} |> in_week?(2015, 1)
true
iex> {2015, 5, 5} |> in_week?(2015, 1)
false
Returns true
if the date
is a Monday.
Examples
iex> {2015, 7, 6} |> monday?
true
iex> {2015, 7, 7} |> monday?
false
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}
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
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}
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
Returns true
if the date
is a Saturday.
Examples
iex> {2015, 7, 11} |> saturday?
true
iex> {2015, 7, 7} |> saturday?
false
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} }
Returns true
if the date
is a Sunday.
Examples
iex> {2015, 7, 12} |> sunday?
true
iex> {2015, 7, 7} |> sunday?
false
Returns true
if the date
is a Thursday.
Examples
iex> {2015, 7, 9} |> thursday?
true
iex> {2015, 7, 7} |> thursday?
false
Takes a Date struct and returns the number of gregorian days since year 0.
iex> from_erl!({2014,12,27}) |> to_gregorian_days
735959
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"
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}
Returns the date for the time right now in UTC.
Examples
> today_utc
%Date{day: 1, month: 3, year: 2016}
Returns true
if the date
is a Tuesday.
Examples
iex> {2015, 7, 6} |> tuesday?
false
iex> {2015, 7, 7} |> tuesday?
true
Returns true
if the date
is a Wednesday.
Examples
iex> {2015, 7, 8} |> wednesday?
true
iex> {2015, 7, 9} |> wednesday?
false
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}