DatesService
katanakit-js / DatesService
Class: DatesService
Section titled “Class: DatesService”Defined in: core/services/dates.service.ts:72
DatesService - Singleton para operaciones de fechas.
Aplica el patrón Singleton: una única instancia reutilizable que centraliza toda la lógica de fechas (diferencias, formateo, aritmética y comparaciones). Puro, sin I/O, seguro para SSR.
Methods
Section titled “Methods”useAddDays()
Section titled “useAddDays()”useAddDays(date, days): string;Defined in: core/services/dates.service.ts:395
Adds a number of days to a date.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
date |
string | PlainDate |
Base date (ISO string or PlainDate). |
days |
number |
Number of days to add (can be negative). |
Returns
Section titled “Returns”string
The resulting date as an ISO string.
Example
Section titled “Example”useAddDays("2024-01-01", 10); // "2024-01-11"useDateAdd()
Section titled “useDateAdd()”useDateAdd( date, amount, unit): string;Defined in: core/services/dates.service.ts:244
Returns the date a given number of units away, preserving calendar semantics — adding one month to the 31st lands on the 31st of a shorter month, not on an overflow.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
date |
TemporalInput |
Starting date. |
amount |
number |
Signed amount; negative moves backwards. |
unit |
"years" | "quarters" | "months" | "weeks" | "days" | "hours" | "minutes" |
Granularity to move by. |
Returns
Section titled “Returns”string
Example
Section titled “Example”useDateAdd("2026-01-31", 1, "months"); // "2026-02-28"useDateAdd(useNow(), 14, "days"); // two weeks outuseDateAdd("2026-01-01", 2, "quarters");// "2026-07-01"useDateBetween()
Section titled “useDateBetween()”useDateBetween( start, end, unit?): number;Defined in: core/services/dates.service.ts:184
Returns the whole number of units between two dates.
Signed, like Notion’s dateBetween: a later end is positive, an
earlier one negative. Never fractional — total() reports 0.0849 years
across a month because a year is not a whole number of months, and a
fractional year count is noise for a calendar API.
Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
start |
TemporalInput |
undefined |
Range start (any TemporalInput). |
end |
TemporalInput |
undefined |
Range end (any TemporalInput). |
unit? |
"years" | "quarters" | "months" | "weeks" | "days" | "hours" | "minutes" |
"days" |
Granularity to report; defaults to days. |
Returns
Section titled “Returns”number
Example
Section titled “Example”useDateBetween("2020-01-01", "2022-04-16"); // 836useDateBetween("2020-01-01", "2022-04-16", "months"); // 27useDateBetween("2022-04-16", "2020-01-01", "days"); // -836useDateOrder()
Section titled “useDateOrder()”useDateOrder(date1, date2): "before" | "after" | "same";Defined in: core/services/dates.service.ts:294
Returns which of two dates comes first.
A three-way comparison is what a date range filter actually needs —
before and after answer different queries, and collapsing them into
a boolean loses the direction you were asked about.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
date1 |
TemporalInput |
date2 |
TemporalInput |
Returns
Section titled “Returns”"before" | "after" | "same"
Example
Section titled “Example”useDateOrder(deadline, useNow()); // "before" | "after" | "same"useDateSubtract()
Section titled “useDateSubtract()”useDateSubtract( date, amount, unit): string;Defined in: core/services/dates.service.ts:249
Subtracts units from a date. The mirror of useDateAdd.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
date |
TemporalInput |
amount |
number |
unit |
"years" | "quarters" | "months" | "weeks" | "days" | "hours" | "minutes" |
Returns
Section titled “Returns”string
useDurationBetween()
Section titled “useDurationBetween()”useDurationBetween(start, end): Duration;Defined in: core/services/dates.service.ts:222
Returns the balanced breakdown of the span between two dates, without collapsing it to prose or to a single number.
The shape is Temporal’s own, so .years, .months and .days are
available separately — which a single formatted string cannot give you
without parsing English out of it.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
start |
TemporalInput |
end |
TemporalInput |
Returns
Section titled “Returns”Duration
Example
Section titled “Example”const span = useDurationBetween("2020-01-01", "2022-04-16");span.years; // 2span.months; // 3span.days; // 15useFirstDayOfMonth()
Section titled “useFirstDayOfMonth()”useFirstDayOfMonth(date?): string;Defined in: core/services/dates.service.ts:477
Returns the first day of the month for a given date.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
date? |
string | PlainDate |
Base date (defaults to today). |
Returns
Section titled “Returns”string
The first day of the month as an ISO string.
Example
Section titled “Example”useFirstDayOfMonth("2024-06-15"); // "2024-06-01"useFormat()
Section titled “useFormat()”useFormat( dateInput, locale?, options?): string;Defined in: core/services/dates.service.ts:320
Formats a date input into a localised string using Intl.DateTimeFormat.
Accepts epoch milliseconds, ISO strings, native Date objects,
Temporal.Instant, or any Temporal date/time type.
Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
dateInput |
TemporalInput |
undefined |
The value to format. |
locale? |
"en" | "es" | "fr" | "de" | "it" | "pt" | "ja" | "zh" |
"en" |
BCP 47 locale tag (defaults to "en"). |
options? |
DateTimeFormatOptions |
{} |
Intl.DateTimeFormatOptions (defaults to {}). |
Returns
Section titled “Returns”string
The formatted date string.
Throws
Section titled “Throws”If the input cannot be parsed as a date.
Example
Section titled “Example”useFormat("2024-06-15"); // "6/15/2024" (en-US)useFormat(Date.now(), "es", { dateStyle: "full" }); // "sábado, 15 de junio de 2024"useIsAfter()
Section titled “useIsAfter()”useIsAfter(date1, date2): boolean;Defined in: core/services/dates.service.ts:462
Checks whether date1 is after date2.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
date1 |
string | PlainDate |
First date (ISO string or PlainDate). |
date2 |
string | PlainDate |
Second date (ISO string or PlainDate). |
Returns
Section titled “Returns”boolean
true if date1 is strictly after date2.
Example
Section titled “Example”useIsAfter("2024-06-15", "2024-01-01"); // trueuseIsAfter("2024-06-15", "2024-06-15"); // falseuseIsBefore()
Section titled “useIsBefore()”useIsBefore(date1, date2): boolean;Defined in: core/services/dates.service.ts:445
Checks whether date1 is before date2.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
date1 |
string | PlainDate |
First date (ISO string or PlainDate). |
date2 |
string | PlainDate |
Second date (ISO string or PlainDate). |
Returns
Section titled “Returns”boolean
true if date1 is strictly before date2.
Example
Section titled “Example”useIsBefore("2024-01-01", "2024-06-15"); // trueuseIsBefore("2024-06-15", "2024-06-15"); // falseuseIsEqual()
Section titled “useIsEqual()”useIsEqual(date1, date2): boolean;Defined in: core/services/dates.service.ts:428
Checks whether two dates are equal.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
date1 |
string | PlainDate |
First date (ISO string or PlainDate). |
date2 |
string | PlainDate |
Second date (ISO string or PlainDate). |
Returns
Section titled “Returns”boolean
true if the dates represent the same day.
Example
Section titled “Example”useIsEqual("2024-06-15", "2024-06-15"); // trueuseIsEqual("2024-06-15", "2024-06-16"); // falseuseLastDayOfMonth()
Section titled “useLastDayOfMonth()”useLastDayOfMonth(date?): string;Defined in: core/services/dates.service.ts:493
Returns the last day of the month for a given date.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
date? |
string | PlainDate |
Base date (defaults to today). |
Returns
Section titled “Returns”string
The last day of the month as an ISO string.
Example
Section titled “Example”useLastDayOfMonth("2024-02-10"); // "2024-02-29" (leap year)useLastDayOfMonth("2024-06-15"); // "2024-06-30"useNow()
Section titled “useNow()”useNow(): string;Defined in: core/services/dates.service.ts:365
Returns today’s date as an ISO date string.
Returns
Section titled “Returns”string
The current date in YYYY-MM-DD format.
Example
Section titled “Example”useNow(); // "2024-06-15"useNowDateTime()
Section titled “useNowDateTime()”useNowDateTime(): string;Defined in: core/services/dates.service.ts:379
Returns the current date and time as an ISO string (no timezone offset).
Returns
Section titled “Returns”string
The current date-time in YYYY-MM-DDTHH:MM:SS format.
Example
Section titled “Example”useNowDateTime(); // "2024-06-15T14:30:00"useSubtractDays()
Section titled “useSubtractDays()”useSubtractDays(date, days): string;Defined in: core/services/dates.service.ts:411
Subtracts a number of days from a date.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
date |
string | PlainDate |
Base date (ISO string or PlainDate). |
days |
number |
Number of days to subtract. |
Returns
Section titled “Returns”string
The resulting date as an ISO string.
Example
Section titled “Example”useSubtractDays("2024-01-11", 10); // "2024-01-01"getInstance()
Section titled “getInstance()”static getInstance(): DatesService;Defined in: core/services/dates.service.ts:91
Obtiene la instancia única del DatesService (Singleton).
Returns
Section titled “Returns”DatesService
La instancia única de DatesService.
Example
Section titled “Example”const dates = DatesService.getInstance();dates.useNow();