Cldr.Calendar.Duration.new

You're seeing just the function new, go back to Cldr.Calendar.Duration module for more information.

Specs

new(interval()) :: {:ok, t()} | {:error, {module(), String.t()}}

Calculates the calendar difference in a Date.Range or CalendarInterval returning a Duration struct.

The difference calculated is in terms of years, months, days, hours, minutes, seconds and microseconds.

Arguments

  • interval is either Date.Range.t() or a CalendarInterval.t()

Returns

  • A {:ok, duration} tuple or a

  • {:error, {exception, reason}} tuple

Notes

Example

iex> Cldr.Calendar.Duration.new(Date.range(~D[2019-01-01], ~D[2019-12-31]))
{:ok,
 %Cldr.Calendar.Duration{
   year: 0,
   month: 11,
   day: 30,
   hour: 0,
   microsecond: 0,
   minute: 0,
   second: 0
 }}

Specs

new(from :: date_or_time_or_datetime(), to :: date_or_time_or_datetime()) ::
  {:ok, t()} | {:error, {module(), String.t()}}

Calculates the calendar difference between two dates returning a Duration struct.

The difference calculated is in terms of years, months, days, hours, minutes, seconds and microseconds.

Arguments

  • from is a date, time or datetime representing the start of the duration.

  • to is a date, time or datetime representing the end of the duration

Notes

  • from must be before or at the same time as to. In addition, both from and to must be in the same calendar

  • If from and to are datetimes then they must both be in the same time zone

Returns

  • A {:ok, duration} tuple or a

  • {:error, {exception, reason}} tuple

Example

iex> Cldr.Calendar.Duration.new(~D[2019-01-01], ~D[2019-12-31])
{:ok,
 %Cldr.Calendar.Duration{
   year: 0,
   month: 11,
   day: 30,
   hour: 0,
   microsecond: 0,
   minute: 0,
   second: 0
 }}