Packages
Lightweight Elixir port of dayjs — pipe-friendly date/time parsing, formatting, manipulation, and querying.
Current section
Files
Jump to
Current section
Files
README.md
# DayEx
Lightweight Elixir port of [dayjs](https://day.js.org/) — pipe-friendly date/time parsing, formatting, manipulation, and querying.
## Installation
Add DayEx to your Mix dependencies. Its Hex package version follows the Backplane release version.
```elixir
def deps do
[
{:day_ex, "~> 1.10"}
]
end
```
Backplane umbrella apps use `{:day_ex, in_umbrella: true}`. To work with named time zones,
set `config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase` in the
application's `config/config.exs`.
## Quick Start
```elixir
DayEx.now()
|> DayEx.add(1, :month)
|> DayEx.start_of(:day)
|> DayEx.format("YYYY-MM-DD")
# => "2026-05-09"
```
## Constructors
```elixir
DayEx.parse(%{year: 2024, month: 3, date: 15, hour: 10})
DayEx.parse([2024, 3, 15, 10, 30])
DayEx.parse(%{"year" => 2024, "month" => 3, "date" => 15, "time_zone" => "America/New_York"})
```
Map and list constructors use DayEx's Elixir-facing convention: months are 1-indexed.
## Conversions
```elixir
d = DayEx.parse!("2024-01-15T10:30:00Z")
DayEx.to_datetime(d)
DayEx.to_naive_datetime(d)
DayEx.to_unix(d)
DayEx.to_unix_millisecond(d)
DayEx.to_map(d)
DayEx.to_list(d)
DayEx.to_date(d)
```
## Calendar time
`DayEx.calendar/1` displays a date relative to today. Pass a reference to get
the same result each time. Dates two through six days away use a weekday label.
Dates seven or more days away use the locale's date format.
```elixir
reference = DayEx.parse!(~D[2024-02-09])
visit = DayEx.parse!(~N[2024-02-10 09:30:00]) |> DayEx.locale(:zh)
DayEx.calendar(visit, reference) # "明天 09:30"
DayEx.calendar(visit, reference, %{next_day: "[预约] HH:mm"}) # "预约 09:30"
DayEx.tomorrow?(visit, reference) # true
DayEx.days_in_month(visit) # 29
```
`today?/2`, `tomorrow?/2` and `yesterday?/2` use the same civil dates as
`calendar/3`. With two zoned values, the reference instant is viewed in the
target's time zone. When either value is naive, both retain their written dates.
The default reference is the current UTC instant.
All seven built-in locales supply calendar text. The optional format map accepts
`:last_week`, `:last_day`, `:same_day`, `:next_day`, `:next_week` and `:same_else`.
Each entry can be a format string or a two-argument function that receives the
target and reference and returns the final text. Omitted entries use the target
locale's defaults. Pass `nil` as the reference to apply overrides relative to now.
## Chinese lunar calendar
Convert Gregorian dates to and from the complete lunar years 1900–2100
(Gregorian 1900-01-31 through 2101-01-28, inclusive). The leap-month flag
distinguishes the two occurrences of a month.
```elixir
{:ok, lunar} = DayEx.to_lunar(~D[2024-02-10])
DayEx.Lunar.format(lunar) # "甲辰年正月初一"
DayEx.Lunar.sexagenary_year(lunar) # "甲辰"
DayEx.Lunar.zodiac(lunar) # "龙"
DayEx.Lunar.to_date(lunar) # {:ok, ~D[2024-02-10]}
{:ok, date} = DayEx.from_lunar(2023, 2, 1, leap_month: true)
DayEx.to_date(date) # ~D[2023-03-22]
DayEx.from_lunar(2023, 2, 30, leap_month: true) # {:error, :invalid_lunar_date}
```
Dates and naive datetimes supply their written civil date. For an instant,
the lunar day changes at midnight in Beijing standard time (UTC+08:00).
The sexagenary year and zodiac change on lunar New Year. Reverse conversion
returns a DayEx value at naive midnight. Dates outside the supported range
return `{:error, :out_of_range}`.
The offline month table derives from the MIT-licensed .NET Chinese lunisolar
calendar. It starts the ninth lunar month of 2057 on September 29 Beijing time.
The Hong Kong Observatory table places it on September 28, so the two tables
differ from September 28 through October 27 of that year.
[Data and attribution](priv/chinese_lunar_years.txt) ·
[.NET calendar correction](https://github.com/dotnet/runtime/issues/107032) ·
[Hong Kong Observatory conversion tables](https://www.hko.gov.hk/en/gts/time/conversion.htm)
## License
MIT