native-dates

Guides / Going further

Coming from Day.js

Replace dayjs() wrappers and plugin chains with dedicated date types and built-in time zones.

Day.js wraps a JavaScript date in a single dayjs() object and adds time zones, UTC, ranges, and durations through plugins. Moving here keeps the lightweight, immutable style while giving calendar days, clock times, moments, partial dates, ranges, and durations their own types in core.

Pick date() or datetime()

In Day.js, both a birthday and an API timestamp go through dayjs().

Here, calendar days use date() or today(), while billing months and recurring birthdays have dedicated partial-date factories in core.

Real moments in time use datetime() or now():

import { date, datetime, monthDay, now, today, yearMonth } from 'native-dates';

const current = now(); // replaces dayjs() for a timestamp
const due = today().plus(2, 'weeks'); // replaces dayjs().add(2, 'week') for a day
const created = datetime('2026-11-16T17:30:00Z');
const birthday = date('2026-05-10');
const billing = yearMonth('2026-11');
const recurring = monthDay('05-10');

Standard addition and subtraction names work as aliases if your codebase already calls them. Passing an empty value to a factory throws instead of silently returning the current time, catching missing API fields early.

Months start at 1

Day.js inherits zero-indexed months where January is 0 and .date() reads the day of the month. Here, calendar fields are plain properties and months run from 1 through 12.

import { date } from 'native-dates';

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

d.year; // 2026
d.month; // 11, November
d.day; // 16
d.dayOfWeek; // 1, Monday

Check month and weekday numbers

Day.js numbers months from 0 to 11 and starts weekdays on Sunday at 0. Here, month runs from 1 through 12, and dayOfWeek runs from 1 on Monday through 7 on Sunday. In token patterns, 'E' prints ISO 1 through 7 while 'd' keeps Day.js’s 0 through 6.

Time zones, durations, and ranges built in

Day.js requires separate plugins before you can convert across cities, build durations, or check whether a date falls inside a window. Core includes IANA time zones, durations, and ranges with zero setup.

import { dateRange, datetime, duration, hours } from 'native-dates';

const standup = datetime('2026-11-16T09:30', 'America/Los_Angeles');
const berlin = standup.in('Europe/Berlin'); // replaces .tz('Europe/Berlin')

const span = duration('1h 30m').plus(hours(0.5));
span.total('minutes'); // 120

const november = dateRange('2026-11-01', '2026-11-30');
november.contains('2026-11-16'); // true
dateRange('2026-11-01', '2026-11-15').overlaps(november); // true

Instead of passing bracket inclusivity strings to isBetween(), date ranges include both calendar endpoints and moment or time ranges exclude their end boundary automatically.

Presets and token patterns

Core supports Intl presets, Day.js localized format names, and custom token patterns directly on format() with zero locale files or plugins:

import { datetime } from 'native-dates';

const dt = datetime('2026-11-16T17:30:00Z', 'America/New_York');

dt.format('medium', 'en-US'); // 'Nov 16, 2026, 12:30 PM'
dt.format('LLL', 'en-US'); // 'November 16, 2026 at 12:30 PM'
dt.format('YYYY-MM-DD HH:mm'); // '2026-11-16 12:30'

Opt-in plugins

Only non-ISO text parsing, city and abbreviation lookups, and legacy date getters live in @native-dates/plugins. Import each subpath once at startup:

import '@native-dates/plugins/loose';
import '@native-dates/plugins/zones';
import { date, datetime } from 'native-dates';

datetime('2026-11-16T09:30', 'Berlin').zone; // 'Europe/Berlin'
date('Nov 16, 2026', { loose: true }); // CalendarDate(2026-11-16)

Side-by-side reference

Day.js Native Dates Notes
dayjs() now() or today() Choose an exact moment or a calendar day
d.month(), d.date() d.month, d.day Plain properties with months from 1 to 12
d.day(), d.isoWeekday() d.dayOfWeek ISO 1 on Monday to 7 on Sunday, with tokens 'E' for 1–7 and 'd' for 0–6
d.unix(), d.valueOf() dt.epochSeconds, dt.epochMilliseconds Each count since 1970 names its unit
d.add(2, 'day') d.plus(2, 'days') add() and subtract() aliases also work
dayjs.tz(x, zone) datetime(x, zone) In core, no timezone plugin
d.tz(zone) dt.in(zone) Views the same moment in another IANA zone
dayjs.utc(x) datetime(x, 'UTC') In core, no utc plugin
b.diff(a, 'day', true) a.until(b, 'days') Fractional total in the requested unit
dayjs.duration(90, 'm') minutes(90) or duration('90m') In core, no duration plugin
d.format('LLL') dt.format('LLL') Localized presets from 'LT' to 'llll'
d.format('YYYY-MM-DD HH:mm') dt.format('YYYY-MM-DD HH:mm') Token patterns in core
d.isBetween(a, b) dateRange(a, b).contains(d) Ranges with to(), for(), and overlaps()
d.fromNow(), d.from(other) d.fromNow(), d.from(other) In core, no relativeTime plugin
customParseFormat { format } or { loose: true } Fixed patterns via @native-dates/plugins/parse, free-form input via @native-dates/plugins/loose
Edit this pageUpdated Nov 20, 2026