native-dates

API / Classes

CalendarDate class

A calendar date with no time of day and no time zone.

import { CalendarDate } from 'native-dates'core 14.9 kB min+br

Creating a CalendarDate

Create an instance from an ISO string, year-month-day numbers, or an existing value with date(), or read the current day with today(). Helpers for tomorrow and yesterday work the same way, and every method returns a new immutable instance.

create.js
import { date, today, tomorrow } from 'native-dates';

const birthday = date('2026-05-10');
const launch = date(2026, 11, 16);
const current = today();
const nextDay = tomorrow('America/New_York');

Because a calendar date has no clock or time zone, May 10 stays May 10 for every user regardless of where they live. You can also read the local calendar date off any DateTime:

import { datetime } from 'native-dates';

const flight = datetime('2026-11-16T23:30:00Z', 'Asia/Tokyo');
flight.date.toString(); // '2026-11-17'

Properties

Properties read the ISO calendar fields directly.

Property Type Description
text string Locale-formatted calendar date, such as 'Nov 16, 2026'.
year, month, day number Calendar year, month (1–12), and day of month (1–31).
yearMonth YearMonth Calendar year and month with the day dropped.
monthDay MonthDay Calendar month and day with the year dropped.
dayOfWeek number ISO day of the week, from 1 for Monday through 7 for Sunday.
weekStart number First day of this date’s weeks from 1 to 7, following configure() or its locale.
quarter number Calendar quarter (1–4).
weekOfYear, dayOfYear number ISO week number (1–53) and day of the year (1–366).
daysInMonth, daysInYear number Number of days in the current month (28–31) and year (365 or 366).

Combining and fields

Attach a time of day in a time zone to create a specific moment, or replace individual calendar fields.

at()

d.at(time?, zone?): DateTime

Combines this calendar date with a time of day in a time zone and returns a DateTime. Omitting the time defaults to midnight at the start of the day.

timeTimeInputdefault '00:00'

Time string such as '9:30am', Time instance, fields object, or Date.

zoneZone

IANA time zone name, fixed UTC offset, or 'UTC'. Defaults to the configured zone.

DateTime

A new DateTime on this date at time in zone.

const day = date('2026-11-16');

day.at('9:30am', 'America/Los_Angeles').toString(); // '2026-11-16T17:30:00.000Z'
day.at(undefined, 'UTC').toString(); // '2026-11-16T00:00:00.000Z'

set()

d.set(fields): CalendarDate
d.set(unit, value): CalendarDate

Returns a new CalendarDate with one or more calendar fields replaced. Out-of-range dates such as February 30 throw a RangeError instead of rolling into the next month. Updating month or year without day clamps an overflowing day to the end of the target month.

fieldsDateTimeFields | DateUnitrequired

Object with year, month, or day fields, or a single unit name.

valuenumber

Whole-number value for the given unit.

CalendarDate

A new CalendarDate with updated calendar fields.

const due = date('2026-11-16');

due.set('day', 1).toString(); // '2026-11-01'
due.set({ month: 12, day: 31 }).toString(); // '2026-12-31'

Arithmetic and boundaries

Step across calendar units, snap to period boundaries, or move to a nearby weekday. Clock units such as hours and minutes throw a RangeError because a calendar date has no clock.

Month-end clamping

Adding months or years clamps to the last valid day of the target month rather than overflowing into the next one. Adding one month to January 31 lands on February 28, or February 29 in a leap year.

plus()

d.plus(amount, unit?): CalendarDate
d.add(amount, unit?): CalendarDate

Moves forward by years, quarters, months, weeks, or days and returns a new CalendarDate. Also aliased as add().

amountDurationInputrequired

Count paired with unit, Duration instance, duration string, or fields object.

unitDateUnit

Calendar unit name when amount is a number, such as 'days' or 'months'.

CalendarDate

A new CalendarDate shifted forward on the calendar.

const billing = date('2026-01-31');

billing.plus(1, 'month').toString(); // '2026-02-28'
billing.plus('2 weeks').toString(); // '2026-02-14'

minus()

d.minus(amount, unit?): CalendarDate
d.subtract(amount, unit?): CalendarDate

Moves backward by a calendar duration. Accepts the same inputs as plus(), and is also aliased as subtract().

amountDurationInputrequired

Count paired with unit, Duration instance, duration string, or fields object.

unitDateUnit

Calendar unit name when amount is a number.

CalendarDate

A new CalendarDate shifted backward on the calendar.

const launch = date('2026-03-01');

launch.minus(1, 'day').toString(); // '2026-02-28'
launch.minus({ weeks: 2, days: 1 }).toString(); // '2026-02-14'

startOf()

d.startOf(unit, firstDay?): CalendarDate

Returns the first day of the calendar unit containing this date. Week boundaries start on firstDay, the configured weekStart from configure(), or this date’s active locale.

unitDateUnitrequired

Calendar unit from 'year' down to 'day'.

firstDayDayOfWeek

First day of the week when rounding to a week, such as 'sunday' or 7.

CalendarDate

The first calendar day of unit.

const wed = date('2026-11-18', { locale: 'en-US' });

wed.startOf('month').toString(); // '2026-11-01'
wed.startOf('quarter').toString(); // '2026-10-01'
wed.startOf('week').toString(); // '2026-11-15'
wed.startOf('week', 'monday').toString(); // '2026-11-16'

endOf()

d.endOf(unit, firstDay?): CalendarDate

Returns the final day of the calendar unit containing this date.

unitDateUnitrequired

Calendar unit from 'year' down to 'day'.

firstDayDayOfWeek

First day of the week when unit is 'week'.

CalendarDate

The last calendar day of unit.

date('2026-02-10').endOf('month').toString(); // '2026-02-28'
date('2028-02-10').endOf('month').toString(); // '2028-02-29'

startOfNext()

d.startOfNext(unit, firstDay?): CalendarDate

Returns the first day of the calendar unit after the one containing this date. The result is always a later date, so from December 1 the next month starts on January 1.

Pass a weekday name in place of a unit to get the next day with that name, the same date next() returns.

unitDateUnit | Exclude<DayOfWeek, number>required

Calendar unit from 'year' down to 'day', or a weekday name such as 'wednesday'.

firstDayDayOfWeek

First day of the week when unit is 'week'.

CalendarDate

The first calendar day of the next unit, or the next given weekday.

const mon = date('2026-11-16', { locale: 'en-US' });

mon.startOfNext('month').toString(); // '2026-12-01'
mon.startOfNext('quarter').toString(); // '2027-01-01'
mon.startOfNext('week').toString(); // '2026-11-22'
mon.startOfNext('wednesday').toString(); // '2026-11-18'

startOfPrevious()

d.startOfPrevious(unit, firstDay?): CalendarDate

Returns the first day of the calendar unit before the one containing this date. A weekday name in place of a unit gives the most recent day with that name, as previous() does.

unitDateUnit | Exclude<DayOfWeek, number>required

Calendar unit from 'year' down to 'day', or a weekday name such as 'wednesday'.

firstDayDayOfWeek

First day of the week when unit is 'week'.

CalendarDate

The first calendar day of the previous unit, or the previous given weekday.

const mon = date('2026-11-16');

mon.startOfPrevious('month').toString(); // '2026-10-01'
mon.startOfPrevious('year').toString(); // '2025-01-01'
mon.startOfPrevious('wednesday').toString(); // '2026-11-11'

next()

d.next(target): CalendarDate

Moves forward to the next given weekday or MonthDay after this date. When already on that weekday or annual date, it advances to the following occurrence.

targetDayOfWeek | MonthDayrequired

Weekday name such as 'friday', ISO number from 1 to 7, or recurring MonthDay.

CalendarDate

A new CalendarDate on the next matching day.

import { date, monthDay } from 'native-dates';

const mon = date('2026-11-16');

mon.next('friday').toString(); // '2026-11-20'
mon.next('monday').toString(); // '2026-11-23'
mon.next(monthDay('12-25')).toString(); // '2026-12-25'

previous()

d.previous(target): CalendarDate

Moves back to the most recent given weekday or MonthDay before this date. When already on that weekday or annual date, it steps back to the prior occurrence.

targetDayOfWeek | MonthDayrequired

Weekday name, three-letter prefix, ISO number from 1 to 7, or recurring MonthDay.

CalendarDate

A new CalendarDate on the previous matching day.

date('2026-11-16').previous('friday').toString(); // '2026-11-13'
date('2026-11-16').previous(monthDay('10-31')).toString(); // '2026-10-31'

Comparing dates

Compare calendar dates directly or pass any input accepted by date().

Avoid numeric comparison operators

A calendar date is not a timestamp, so numeric operators throw a RangeError rather than coercing to milliseconds. Compare dates with methods like isBefore(), or sort arrays with compare().

equals()

d.equals(other): boolean

Returns true when both values represent the same calendar year, month, and day.

otherCalendarDateInputrequired

Another CalendarDate or any input accepted by date().

boolean

true if both values represent the same calendar day.

const due = date('2026-11-16');

due.equals('2026-11-16'); // true
due.equals(date(2026, 11, 16)); // true

isBefore()

d.isBefore(other): boolean

Returns true if this date is earlier on the calendar than the other.

date('2026-11-15').isBefore('2026-11-16'); // true

isAfter()

d.isAfter(other): boolean

Returns true if this date is later on the calendar than the other.

date('2026-11-17').isAfter('2026-11-16'); // true

isSame()

d.isSame(other, unit?, firstDay?): boolean

Checks whether another date falls in the same calendar unit, such as the same month or week. Calling isSame() without a unit checks whether both values are the exact same day.

otherCalendarDateInputrequired

Date to compare against.

unitDateUnit

Calendar unit to compare, from 'year' down to 'day'.

firstDayDayOfWeek

First day of the week when unit is 'week'.

boolean

true when both dates fall in the same unit.

const wed = date('2026-09-09', { locale: 'en-US' });

wed.isSame('2026-09-30', 'month'); // true
wed.isSame('2026-09-06', 'week'); // true
wed.isSame('2026-09-06', 'week', 'monday'); // false

Calendar predicates

Relative day checks accept an optional time zone because which calendar day is today depends on where you stand. When omitted, they use the default zone from configure().

Method Type Description
isToday(zone?), isTomorrow(zone?), isYesterday(zone?) boolean Whether this date matches today, tomorrow, or yesterday in the given or configured zone.
isPast(zone?), isFuture(zone?) boolean Whether this date is earlier or later than today in the given or configured zone.
isWeekday(), isWeekend() boolean Working day vs. weekend day in the active locale, such as Saturday and Sunday in en-US or Friday and Saturday in ar-SA.
isLeapYear() boolean Whether this date’s year has 366 days.

Measuring spans

until()

d.until(other): Duration
d.until(other, unit): number

Measures the calendar span from this date to another, returning a negative duration when the target is earlier. The returned Duration remembers this starting date so months and years can be totaled accurately.

otherCalendarDateInputrequired

Target date to measure toward.

unitUnit

Unit to return the total length in as a floating-point number.

Duration | number

A Duration counted in days, or a number when unit is given.

const start = date('2026-01-01');

start.until('2027-04-05').toString(); // 'P459D'
start.until('2027-04-05').balance('year').toString(); // 'P1Y3M4D'
start.until('2027-04-05', 'days'); // 459
start.until('2026-07-01').total('months'); // 6

since()

d.since(other): Duration
d.since(other, unit): number

Measures the calendar span elapsed since an earlier date. Pass a unit name to get the total as a number.

date('2026-11-16').since('2026-11-02', 'weeks'); // 2

to()

d.to(end): DateRange
d.to(count, unit): DateRange
d.through(end): DateRange
d.through(count, unit): DateRange
d.for(length): DateRange
d.for(count, unit): DateRange

Creates an inclusive DateRange starting on this date and running through the end date or span length. Both the start day and end day are included. Also aliased as through(); use for() to pass an explicit duration length (refusing endpoints and unitless numbers).

endCalendarDateInput | DurationInput | numberrequired

Final date to include, calendar duration, or numeric count paired with unit.

unitDateUnit

Calendar unit name when the first argument is a numeric count, such as 'days' or 'weeks'.

DateRange

An inclusive DateRange starting on this date.

date('2026-11-01').to('2026-11-07').duration.days; // 7
date('2026-11-01').for(7, 'days').toString(); // '2026-11-01/2026-11-07'

range()

d.range(unit, firstDay?): DateRange

Returns an inclusive DateRange covering every day of the calendar unit containing this date, from its first day through its last.

unitDateUnitrequired

Calendar unit from 'year' down to 'day'.

firstDayDayOfWeek

First day of the week when unit is 'week'.

DateRange

An inclusive DateRange from the start through the end of unit.

date('2026-02-10').range('month').toString(); // '2026-02-01/2026-02-28'
date('2026-11-18', { locale: 'en-US' }).range('week').toString(); // '2026-11-15/2026-11-21'
date('2026-11-18').range('week', 'monday').toString(); // '2026-11-16/2026-11-22'

every()

d.every(step, zone?): DateTime[]
d.every(count, unit, zone?): DateTime[]

Walks through this calendar day by a positive step in the given or configured time zone, returning an array of DateTime moments starting at midnight. Daylight saving transitions produce 23 or 25 hourly points automatically.

stepUnit | DurationInput | numberrequired

Unit name such as 'hour', duration such as '30m', or numeric count paired with unit.

unitUnit

Unit name when the first argument is a numeric count.

zoneZone

Time zone for the day's boundaries, defaulting to the configured zone.

DateTime[]

Moments across this calendar day in zone.

const slots = date('2026-11-16').every(6, 'hours', 'UTC');

slots.length; // 4
slots[1].toString(); // '2026-11-16T06:00:00.000Z'

split()

d.split(step, zone?): DateTimeRange[]
d.split(count, unit, zone?): DateTimeRange[]
d.splitBy(step, zone?): DateTimeRange[]
d.splitBy(count, unit, zone?): DateTimeRange[]

Divides this calendar day into contiguous half-open DateTimeRange intervals in the given or configured time zone. It is also aliased as splitBy().

stepUnit | DurationInput | numberrequired

Unit name, duration, or numeric count paired with unit.

unitUnit

Unit name when the first argument is a numeric count.

zoneZone

Time zone for the day's boundaries, defaulting to the configured zone.

DateTimeRange[]

Contiguous DateTimeRange slices covering this day in zone.

const halves = date('2026-11-16').split(12, 'hours', 'UTC');

halves.map(String);
// ['2026-11-16T00:00:00.000Z/2026-11-16T12:00:00.000Z', '2026-11-16T12:00:00.000Z/2026-11-17T00:00:00.000Z']

Formatting and conversion

format()

d.format(spec?, locale?): string

Formats the date using built-in presets, custom names from configure(), options objects, or token patterns.

Localized presets from 'L' through 'LLLL' are built in, and options objects ignore clock and zone keys automatically. See Formatting for the full preset and token reference.

specFormatSpecdefault 'medium'

Built-in or configured format name, Intl options object, or token pattern.

localestring

BCP 47 locale tag overriding this date's or the configured locale for one call.

string

Formatted date string.

const day = date('2026-11-16');

day.format('short', 'en-US'); // '11/16/26'
day.format('medium', 'en-US'); // 'Nov 16, 2026'
day.format('long', 'en-US'); // 'November 16, 2026'
day.format('full', 'en-US'); // 'Monday, November 16, 2026'
day.format('L', 'en-US'); // '11/16/2026'
day.format('YYYY-MM-DD'); // '2026-11-16'
day.format('ddd, MMM D', 'en-US'); // 'Mon, Nov 16'
day.format({ weekday: 'long' }, 'en-US'); // 'Monday'

from()

d.from(other?, locale?): string
d.formatRelative(other?, locale?): string

Formats the calendar distance from another date to this one as a localized phrase via Intl.RelativeTimeFormat, such as 'yesterday' or 'in 3 weeks'.

Omitting the first argument compares against today(), and formatRelative() works as an alias. Distances under seven days use natural words for yesterday, today, and tomorrow, while weeks, months, and years use numeric counts.

otherCalendarDateInput

Reference calendar date to measure from. Defaults to today() in the active zone.

localeLocale

BCP 47 locale tag overriding this date's or the configured locale for one call.

string

Localized relative phrase.

const anchor = date('2026-11-16');

date('2026-11-15').from(anchor, 'en-US'); // 'yesterday'
date('2026-11-16').from(anchor, 'en-US'); // 'today'
date('2026-11-17').from(anchor, 'en-US'); // 'tomorrow'
date('2026-11-30').from(anchor, 'en-US'); // 'in 2 weeks'
date('2026-11-15').from(anchor, 'de'); // 'gestern'

fromNow()

d.fromNow(locale?): string

Formats how far this date is from today as a localized phrase, equivalent to calling from() with no reference date.

localeLocale

BCP 47 locale tag overriding this date's or the configured locale for one call.

string

Localized relative phrase measured from today.

today().minus(1, 'day').fromNow('en-US'); // 'yesterday'
today().plus(3, 'days').fromNow('en-US'); // 'in 3 days'

formatRelative()

d.formatRelative(other?, locale?): string

Alias for from() that formats the calendar distance from other, or from today when omitted, as a localized phrase.

locale()

d.locale(): Locale | undefined
d.locale(tag): CalendarDate

Reads the locale carried on this date, or returns a copy that formats and starts weeks in another locale across presets, week boundaries, and derived values. Underscore tags and Accept-Language headers normalize automatically, an empty tag clears it, and lang() is an alias.

tagLocale | null | undefined

Locale tag, tag array, or Accept-Language header to carry forward, or null to clear it.

CalendarDate | Locale | undefined

The active tag when called with no arguments, or a new CalendarDate carrying tag.

const trip = date('2026-05-15').locale('fr-FR');

trip.locale(); // 'fr-FR'
trip.format('long'); // '15 mai 2026'
trip.to('2026-05-19').format('long'); // '15–19 mai 2026'

lang()

d.lang(): Locale | undefined
d.lang(tag): CalendarDate

Shorthand alias for locale() that reads or attaches a BCP 47 locale tag on this date.

as()

d.as('date'): CalendarDate
d.as('datetime', zone?): DateTime
d.as('yearMonth'): YearMonth
d.as('monthDay'): MonthDay

Converts this date to another kind inside a method chain, including YearMonth and MonthDay. Converting to a moment returns midnight at the start of this date in the given or configured zone.

kind'date' | 'datetime' | 'yearMonth' | 'monthDay'required

Target kind to read this calendar date as.

zoneZone

Time zone used when converting to 'datetime', defaulting to the configured zone.

CalendarDate | DateTime | YearMonth | MonthDay

Converted value.

const due = date('2026-11-16');

due.as('datetime', 'UTC').toString(); // '2026-11-16T00:00:00.000Z'
due.as('yearMonth').toString(); // '2026-11'
due.as('monthDay').toString(); // '11-16'

toString()

d.toString(): string
d.toJSON(): string

Returns the date as an ISO 8601 YYYY-MM-DD string. toJSON() returns the same string for JSON.stringify().

string

ISO 8601 date string, such as '2026-11-16'.

const due = date('2026-11-16');

due.toString(); // '2026-11-16'
JSON.stringify({ due }); // '{"due":"2026-11-16"}'

toJSDate()

d.toJSDate(zone?): Date

Returns a native JavaScript Date at midnight on this date in the given or configured time zone.

toTemporal()

d.toTemporal(): Temporal.PlainDate

Returns the underlying Temporal.PlainDate instance.

valueOf()

d.valueOf(): never

Throws a RangeError so numeric operators never silently coerce a calendar date to a number.

Edit this pageUpdated Nov 20, 2026