Guides / Working with time
Parsing
Read ISO strings, durations, ranges, and loose user input without silent guesswork.
Core parses standard, unambiguous inputs directly into the type you ask for:
datetime()for moments in time, epoch milliseconds, and timestampsdate()for calendar days with no clock or time zonetime()for 24-hour and 12-hour times of dayduration()for human unit phrases and ISO 8601 durationsyearMonth()andmonthDay()for billing months, card expiries, and recurring birthdaysdateRange(),datetimeRange(), andtimeRange()for intervals and endpoint pairs
ISO dates and timestamps
Pass a year-first date string, three year, month, and day numbers, or a fields object to date(). Year-first strings accept padded or single-digit months and days with any separator other than Latin letters or colons. Chinese, Japanese, Korean, and spaced dot dates all read directly in core.
Months run from 1 through 12. Impossible calendar dates like February 30 and ISO week or quarter strings like '2026-W05-1' throw an error instead of guessing.
import { date } from 'native-dates';
date('2026-11-16');
date('2026/11/6');
date('2026年11月16日');
date('2026. 11. 16.');
date(2026, 11, 16);
date({ year: 2026, month: 11, day: 16 });Pass ISO 8601 strings or fields objects to datetime(). A trailing Z or numeric offset identifies the exact moment, while a string without an offset reads as local time in the target zone.
import { datetime } from 'native-dates';
datetime('2026-11-16T17:30:00Z', 'America/Los_Angeles'); // 9:30 AM PST
datetime('2026-11-16T09:30', 'America/Los_Angeles'); // 9:30 AM PST
datetime('2026-11-16T09:30[Europe/Berlin]'); // 9:30 AM CET
datetime({ year: 2026, month: 11, day: 16, hour: 9, minute: 30 }, 'Europe/Berlin');Passing an offset timestamp to date() or time() reads its calendar day or local clock in the time zone you ask for:
import { time } from 'native-dates';
date('2026-11-17T01:30:00Z', 'America/Los_Angeles'); // 2026-11-16
date('2026-11-17T01:30:00Z', 'Asia/Tokyo'); // 2026-11-17
time('2026-11-17T01:30:00Z', 'America/Los_Angeles'); // 17:30:00Clock times and durations
Pass 24-hour ISO times, hour-minute strings like '14h30', 12-hour clock strings, numeric arguments, or a fields object to time(). A bare hour like '14h' still throws so durations stay unambiguous.
import { time } from 'native-dates';
time('09:30');
time('14h30');
time('9:30am');
time('5:15 PM');
time(17, 15, 30);
time({ hour: 9, minute: 30 });Pass plain-English unit phrases, ISO 8601 duration strings, a count and unit, or a fields object to duration().
import { duration } from 'native-dates';
duration('1h 30m');
duration('2 weeks and 3 days');
duration('PT90M');
duration(90, 'minutes');
duration({ hours: 1, minutes: 30 });Partial dates and ranges
Pass ISO year-month strings to yearMonth(), or read card expiries where the month leads with monthYear():
import { dayMonth, monthDay, monthYear, yearMonth } from 'native-dates';
yearMonth('2026-11');
monthYear('11/28'); // YearMonth(2028-11)
monthDay('10/17');
dayMonth('17/10'); // MonthDay(10-17)Recurring dates without a year work the same way through monthDay() and dayMonth(). For spans, range factories read two endpoints or a single interval string:
import { dateRange, timeRange } from 'native-dates';
dateRange('2026-11-01/2026-11-07');
timeRange('9am - 5pm');Epoch timestamps, Date, and Temporal
Epoch milliseconds, native JavaScript dates, and Temporal objects pass straight into datetime().
Passing a bare number to a date factory throws as ambiguous. Convert epoch milliseconds to a moment in a zone first, then read its calendar day.
import { date, datetime } from 'native-dates';
const fromEpoch = datetime(1794850200000, 'UTC');
const fromJSDate = datetime(new Date('2026-11-16T17:30:00Z'));
const localDay = date(new Date('2026-11-16T17:30:00Z'), 'America/Los_Angeles');Why ambiguous strings throw
While a four-digit year at the front has only one reading, '03/05/2026' means March 5 in the United States and May 3 across Europe and Latin America. Core refuses ambiguous numeric strings rather than guessing the wrong month in production.
User input with the loose plugin
When parsing free-form text from user inputs, CSV exports, or RFC 2822 headers, import @native-dates/plugins/loose and pass { loose: true }. Loose mode reads dates and clock times the way the active locale writes them, including localized month names, day-period words, dotted clocks, and non-ASCII digits.
import '@native-dates/plugins/loose';
import { date, datetime, monthDay, time, yearMonth } from 'native-dates';
date('Nov 16, 2026', { loose: true }); // 2026-11-16
date('16 de noviembre de 2026', { loose: true, locale: 'es' }); // 2026-11-16
datetime('Mon, 16 Nov 2026 17:30:00 GMT', { loose: true });
time('오후 2:30', { loose: true, locale: 'ko' }); // 14:30:00
yearMonth('Nov 2026', { loose: true }); // 2026-11
monthDay('17 février', { loose: true, locale: 'fr' }); // 02-17Numeric slash and dot dates follow the active locale’s order. Pass dayFirst per call or set it in app configuration when an input uses a different order than the active locale.
date('03/05/2026', { loose: true, locale: 'en-US' }); // 2026-03-05
date('03/05/2026', { loose: true, locale: 'fr' }); // 2026-05-03
date('03/05/2026', { loose: true, locale: 'en-US', dayFirst: true }); // 2026-05-03Under loose parsing, unreadable inputs return null instead of throwing across every factory and range. Invalid option values like an unknown time zone name still throw.
import { duration } from 'native-dates';
date('not a date', { loose: true }); // null
duration('soon', { loose: true }); // nullWhen inputs follow a known non-ISO layout such as CSV exports ('24112026'), server logs, or week and ordinal dates, import @native-dates/plugins/parse and pass { format } for deterministic token matching instead of heuristic parsing.
import '@native-dates/plugins/parse';
import { date, datetime } from 'native-dates';
date('24112026', { format: 'DDMMYYYY' }); // 2026-11-24
datetime('24/Nov/2026:12:40:01 -0500', { format: 'DD/MMM/YYYY:HH:mm:ss ZZ', locale: 'en-US' });