Datex.Date (datex v2.0.0)
Date provides simple date formats to work with and manipulate any date formats to either elixir or any nicer looking dates.
Summary
Functions
Add days to date.
Returns the first date of the month for the given date.
Returns the number of business days (Monday-Friday) between two dates.
It compares 2 dates
and provides us human friendly results like 2 days later
or 6 months ago
.
It checks whether a particular date
falls inside a DateRange
.
It produces an array of dates in formats you want.
Get day of week as Integer
for a particular date in any format. Monday
is 1
and Sunday
as 7
.
Returns shory name of day as String
for a particular date in any format.
Get number of days in a month as Integer
for a particular date in any format.
Get difference between two dates as Integer
.
Returns the last date of the month for the given date.
Change given date
to a specific date format.
Returns true if the year of the given date is a leap year.
Returns true if the given date is a Saturday or Sunday.
Get month name as String
for a particular date in any format.
Returns the next date for the given weekday after the given date. Weekday can be a number (1-7), full day name, or short day name. Numbers: 1 (Monday) through 7 (Sunday) Names: "Monday", "Mon", "monday", "mon", etc. If no date is provided, defaults to today. Optional format parameter: :elixir, :nice_date, :nice_short_date
Returns the nth occurrence of a specific weekday in a given month and year. Weekday can be a number (1-7), full day name, or short day name. Numbers: 1 (Monday) through 7 (Sunday) Names: "Monday", "Mon", "monday", "mon", etc. n can be 1-5 (1st, 2nd, 3rd, 4th, 5th occurrence) Optional format parameter: :elixir, :nice_date, :nice_short_date
Returns the quarter (1-4) for the given date.
Get the range between two dates as DateRange
.
Get current date with or without time zone.
Get tomorrow's date with or without time zone in similar syntax as today.
Returns the ISO week number for the given date.
Get year as Integer
for a particular date in any format.
Get yesterday's date with or without time zone in similar syntax as today/tomorrow.
Functions
Add days to date.
It takes 2 arguments and an optional 3rd argument. First argument is Date
in elixir date format or standard date as String
, second is number of days
.
Third argument is format like :nice_date
, :elixir
, :nice_short_date
.
It returns date in formats specified in today()
function.
Examples
iex()> Datex.Date.add("15 Oct, 2018", 10)
"25 October, 2018"
iex()> Datex.Date.add("20/10/2018", 13, :elixir)
~D[2018-11-02]
iex()> Datex.Date.add("15 Oct, 2018", 10, :nice_short_date)
"25 Oct, 2018"
Returns the first date of the month for the given date.
Examples
iex> Datex.Date.beginning_of_month("15 Sept, 2018")
~D[2018-09-01]
iex> Datex.Date.beginning_of_month("1 Jan, 2020")
~D[2020-01-01]
Returns the number of business days (Monday-Friday) between two dates.
Examples
iex> Datex.Date.business_days_between("1 Jan, 2018", "5 Jan, 2018")
5
iex> Datex.Date.business_days_between("1 Jan, 2018", "7 Jan, 2018")
5
It compares 2 dates
and provides us human friendly results like 2 days later
or 6 months ago
.
It takes 2 arguments. First is a date
and second is also a date
defaulted to today
.
It returns friendly result as string
compare/1
will compare the given date
from today itself. You can even pass second argument as date
to compare with.
Examples
iex()> Datex.Date.compare("15 aug 2018")
"a month and 1 day ago"
iex()> Datex.Date.compare("15 aug 2018", "20/05/2017")
"a year and 2 months later"
iex()> Datex.Date.compare("15-08-2018", "20/05/2010")
"8 years later"
iex(10)> Datex.Date.compare("15 aug 2018", ~D[2018-08-06])
"a week and 2 days later"
It checks whether a particular date
falls inside a DateRange
.
It takes 2 arguments. First is a DateRange
and second is date
.
It returns boolean either true
or false
. You can iterate over DateRange
for other date functionalities.
Examples
iex()> range = Datex.Date.range(~D[2018-10-17], "12 Dec 2019")
#DateRange<~D[2018-10-17], ~D[2019-12-12]>
iex()> Datex.Date.date_in_range(range, "12/03/2019")
true
iex()> Datex.Date.date_in_range(range, "12/03/2021")
false
iex()> for date <- range, do: date
[~D[2018-10-17], ~D[2018-10-18], ~D[2018-10-19], ~D[2018-10-20], ~D[2018-10-21],
~D[2018-10-22], ~D[2018-10-23], ~D[2018-10-24], ~D[2018-10-25], ~D[2018-10-26],
~D[2018-10-27], ~D[2018-10-28], ~D[2018-10-29], ~D[2018-10-30], ~D[2018-10-31],
...]
It produces an array of dates in formats you want.
It takes 2 arguments. First is a DateRange
and second is format
which is optional and gives elixir date by default.
Formats are specified in format_date()
It returns an array of dates in the given format.
Examples
range = Datex.Date.range("10 sept, 2018", "20 sept, 2018")
iex()> Datex.Date.date_list(range, "DD-MM-YYYY")
["10-09-2018", "11-09-2018", "12-09-2018", "13-09-2018", "14-09-2018",
"15-09-2018", "16-09-2018", "17-09-2018", "18-09-2018", "19-09-2018",
"20-09-2018"]
iex()> Datex.Date.date_list(range, "DAY_SHORT, DATE MONTH_NAME_SHORT")
["Mon, 10 Sept", "Tue, 11 Sept", "Wed, 12 Sept", "Thu, 13 Sept",
"Fri, 14 Sept", "Sat, 15 Sept", "Sun, 16 Sept", "Mon, 17 Sept",
"Tue, 18 Sept", "Wed, 19 Sept", "Thu, 20 Sept"]
iex()> Datex.Date.date_list(range)
[~D[2018-09-10], ~D[2018-09-11], ~D[2018-09-12], ~D[2018-09-13],
~D[2018-09-14], ~D[2018-09-15]]
Get day as String
for a particular date in any format.
For example date formats like DD-MM-YYYY
, 10th Oct, 2019
, YYYY-MM-DD
,
DD/MM/YYYY
, DD.MM.YYYY
or even elixir dates.
Examples
iex()> Datex.Date.day("12 Sept, 2017")
"Tuesday"
iex()> Datex.Date.day("12/7/2018")
"Thursday"
iex()> Datex.Date.day(~D[1997-02-23])
"Sunday"
Get day of week as Integer
for a particular date in any format. Monday
is 1
and Sunday
as 7
.
Examples
iex()> Datex.Date.day_of_week("15th Sept 2018")
6
Returns shory name of day as String
for a particular date in any format.
For example date formats like DD-MM-YYYY
, 10th Oct, 2019
, YYYY-MM-DD
,
DD/MM/YYYY
, DD.MM.YYYY
or even elixir dates.
Examples
iex()> Datex.Date.day_short("12 Sept, 2017")
"Tue"
Get number of days in a month as Integer
for a particular date in any format.
Examples
iex()> Datex.Date.days_in_month("2018/09/25")
30
Get difference between two dates as Integer
.
It takes 2 different dates as arguments. You can specify 2 dates in different format as well.
Other than elixir_date
format remaining formats should be string
.
Examples
iex(1)> Datex.Date.difference("25 oct 2018", "10/10/2018")
15
iex(2)> Datex.Date.difference(~D[2018-11-25], "10-10-2018")
46
Returns the last date of the month for the given date.
Examples
iex> Datex.Date.end_of_month("15 Sept, 2018")
~D[2018-09-30]
iex> Datex.Date.end_of_month("1 Feb, 2020")
~D[2020-02-29]
Change given date
to a specific date format.
It takes 2 arguments. First is a Date
and second is format
as String
you want to convert to.
It returns date
in required format.
Valid formats are:
DD-MM-YYYY
YYYY-MM-DD
DD-MM-YY
DD/MM/YYYY
YYYY/MM/DD
DD/MM/YY
DD.MM.YYYY
YYYY.MM.DD
DD.MM.YY
DATE MONTH_NAME_FULL YYYY
DATE MONTH_NAME_SHORT YYYY
DATE MONTH_NAME_SHORT YY
MONTH_NAME_FULL DATE YYYY
MONTH_NAME_SHORT DATE YYYY
DAY_FULL, DATE MONTH_NAME_FULL YYYY
DAY_SHORT, DATE MONTH_NAME_SHORT YYYY
DAY, DATE MONTH_NAME_FULL
DAY_SHORT, DATE MONTH_NAME_SHORT
Examples
iex()> Datex.Date.format_date("10 Feb, 2017", "YYYY/MM/DD")
"2017/02/10"
iex()> Datex.Date.format_date("10 feb, 2017", "MONTH_NAME_FULL DATE YYYY")
"February 10, 2017"
iex()> Datex.Date.format_date("10 feb, 2017", "DAY_FULL, DATE MONTH_NAME_FULL YYYY")
"Friday, 10 February 2017"
iex()> Datex.Date.format_date(~D[2019-02-10], "DAY_SHORT, DATE MONTH_NAME_SHORT")
"Sun, 10 Feb"
Returns true if the year of the given date is a leap year.
Examples
iex> Datex.Date.is_leap_year?("29 Feb, 2020")
true
iex> Datex.Date.is_leap_year?("29 Feb, 2021")
false
Returns true if the given date is a Saturday or Sunday.
Examples
iex> Datex.Date.is_weekend?("15 Sept, 2018")
true
iex> Datex.Date.is_weekend?("14 Sept, 2018")
false
Get month name as String
for a particular date in any format.
Examples
iex()> Datex.Date.month("2018-9-25")
"September"
Returns the next date for the given weekday after the given date. Weekday can be a number (1-7), full day name, or short day name. Numbers: 1 (Monday) through 7 (Sunday) Names: "Monday", "Mon", "monday", "mon", etc. If no date is provided, defaults to today. Optional format parameter: :elixir, :nice_date, :nice_short_date
Examples
iex> Datex.Date.next_weekday("Friday")
~D[2018-09-21]
iex> Datex.Date.next_weekday("15 Sept, 2018", "Mon")
~D[2018-09-17]
iex> Datex.Date.next_weekday("15 Sept, 2018", "Friday", :nice_date)
"21 September, 2018"
iex> Datex.Date.next_weekday("15 Sept, 2018", 5)
~D[2018-09-21]
Returns the nth occurrence of a specific weekday in a given month and year. Weekday can be a number (1-7), full day name, or short day name. Numbers: 1 (Monday) through 7 (Sunday) Names: "Monday", "Mon", "monday", "mon", etc. n can be 1-5 (1st, 2nd, 3rd, 4th, 5th occurrence) Optional format parameter: :elixir, :nice_date, :nice_short_date
Examples
iex> Datex.Date.nth_weekday_of_month(3, "Monday", "November", 2028)
~D[2028-11-20]
iex> Datex.Date.nth_weekday_of_month(2, "Tuesday", "Jan", 2030, :nice_date)
"8 January, 2030"
iex> Datex.Date.nth_weekday_of_month(1, 5, 12, 2024) # 1st Friday of December 2024
~D[2024-12-06]
Returns the quarter (1-4) for the given date.
Examples
iex> Datex.Date.quarter("15 Jan, 2018")
1
iex> Datex.Date.quarter("15 Apr, 2018")
2
iex> Datex.Date.quarter("15 Jul, 2018")
3
iex> Datex.Date.quarter("15 Oct, 2018")
4
Get the range between two dates as DateRange
.
It takes 2 different dates as arguments. You can specify 2 dates in different format as well.
Other than elixir_date
format remaining formats should be string
.
Examples
iex(1)> Datex.Date.range("12-12-2018", "12 Dec 2019")
#DateRange<~D[2018-12-12], ~D[2019-12-12]>
iex(2)> Datex.Date.range(~D[2018-10-17], "12 Dec 2019")
#DateRange<~D[2018-10-17], ~D[2019-12-12]>
Get current date with or without time zone.
It takes 2 optional parameters i.e, time_zone
as first params and format
as second.
Without any params ie today/0 will return utc_date in a nicer format.
today/1 will need a time_zone which is specified in timezones.
For a different format other than :nice_date
like :elixir
or :nice_short_date
,
You need to specify first argument along with format as second argument. Pass :utc
as first argument for default.
Examples
iex()> Datex.Date.today()
"15 September, 2018"
iex()> Datex.Date.today("Pacific/Samoa")
"15 September, 2018"
iex()> Datex.Date.today("Pacific/Samoa", :elixir)
~D[2018-09-15]
iex()> Datex.Date.today(:utc, :nice_short_date)
"15 Sept, 2018"
Get tomorrow's date with or without time zone in similar syntax as today.
Examples
iex()> Datex.Date.tomorrow()
"16 September, 2018"
iex()> Datex.Date.tomorrow("Asia/Calcutta", :elixir)
~D[2018-09-16]
Returns the ISO week number for the given date.
Examples
iex> Datex.Date.week_number("1 Jan, 2018")
1
iex> Datex.Date.week_number("31 Dec, 2018")
1
Get year as Integer
for a particular date in any format.
Examples
iex()> Datex.Date.year("5th Jan, 2030")
2030
Get yesterday's date with or without time zone in similar syntax as today/tomorrow.
Examples
iex()> Datex.Date.yesterday()
"14 September, 2018"
iex(4)> Datex.Date.yesterday("Asia/Calcutta", :nice_short_date)
"14 Sept, 2018"