Guides / Working with time
Types of time
Pick the right type for a calendar day, a time of day, a specific moment, a partial date, a range, or a length of time.
JavaScript’s built-in Date uses one object for everything, whether you mean someone’s birthday, a 9:30 AM alarm, or the exact second an order was placed.
Each concept gets its own type so you only work with the parts you actually have:
CalendarDatefor a day on the calendar, like May 10, 2026Timefor a time of day, like 9:30 AMDateTimefor a specific moment in a time zoneDurationfor a length of time, like 2 hours or 3 weeksYearMonthandMonthDayfor a month without a day, or a recurring day without a year- Ranges for a span between two dates, times, or moments
CalendarDate
Use CalendarDate when something happens on a day rather than at a specific second: birthdays, due dates, holidays, or values from a date picker.
Create one with today() or date():
import { date, today } from 'native-dates';
const birthday = date('2026-05-10');
const due = today().plus(2, 'weeks');
birthday.month; // 5
birthday.day; // 10Because a calendar date has no clock or time zone, May 10 stays May 10 for every user regardless of where they live.
Time
Use Time for a time of day that isn’t tied to one specific date, such as store opening hours or a daily reminder.
import { time } from 'native-dates';
const opens = time('9:00am');
const closes = opens.plus(8, 'hours');
closes.hour; // 17
closes.format('short', 'en-US'); // '5:00 PM'Adding hours past midnight wraps around the 24-hour clock cleanly, since there is no date to advance.
DateTime
Use DateTime when something happens at a real point in time: a meeting, a flight departure, or a timestamp from your database.
Create one with now() or datetime(), or attach a time to a calendar date:
import { date, datetime, now } from 'native-dates';
const current = now();
const created = datetime('2026-11-16T17:30:00Z');
const standup = date('2026-11-16').at('9:30am', 'America/Los_Angeles');Each moment knows both when something happens and which time zone you’re viewing it in. Call in() to see that same moment in another city:
standup.in('Europe/Berlin').format('time', 'en-US'); // '6:30 PM'Duration
Use Duration for how long something lasts. Create one with unit helpers like hours() or pass a phrase to duration():
import { days, duration, hours } from 'native-dates';
const call = hours(1.5);
const trial = days(14);
const turnaround = duration('2 weeks, 3 days');Measuring the gap between two values returns a duration too:
const opens = time('9:00am');
const lunch = time('12:30pm');
opens.until(lunch).format('long', 'en-US'); // '3 hours, 30 minutes'YearMonth
Use YearMonth when a value has a year and a month with no day, such as a billing period, a card expiry, or a month input.
import { monthYear, yearMonth } from 'native-dates';
const billing = yearMonth('2026-11');
const expiry = monthYear('11/28');
billing.daysInMonth; // 30
billing.on(15); // CalendarDate for 2026-11-15It counts in months and years, and on() picks any day inside that month as a calendar date.
MonthDay
Use MonthDay for a recurring date without a year, such as a birthday or an annual holiday.
import { monthDay } from 'native-dates';
const birthday = monthDay('10/17');
birthday.in(2026); // CalendarDate for 2026-10-17
birthday.fallsOn('2026-10-17'); // trueFebruary 29 lands on February 28 in non-leap years automatically when you call fallsOn() or in().
Ranges
Use Ranges for a span between two dates, times, or moments. Create one directly with dateRange(), timeRange(), or datetimeRange():
import { dateRange, timeRange } from 'native-dates';
const sprint = dateRange('2026-11-02', '2026-11-15');
const shift = timeRange('9:00am', '5:00pm');
sprint.contains('2026-11-10'); // true
shift.duration.hours; // 8Date ranges include their final day, while moment and time ranges exclude their end boundary so back-to-back slots never overlap.
Moving between them
These types connect directly when you add or drop parts. Attach a time to a calendar date with at() to get a moment, or span from an existing point with for() or to():
const reminder = today().plus(1, 'day').at('9:00am');
const upcoming = today().for(2, 'weeks');Going the other way, read part properties directly off any moment or date, or call as() inside a method chain:
reminder.date; // CalendarDate for tomorrow
reminder.time; // Time for 09:00:00
reminder.yearMonth; // YearMonth
reminder.monthDay; // MonthDay
reminder.in('Asia/Tokyo').as('time'); // Time in TokyoDateTime or CalendarDate?
Ask whether changing time zones should change what the user sees. A 9:30 AM call in Los Angeles is 6:30 PM in Berlin, so it belongs in DateTime. A birthday on May 10 is still May 10 everywhere, so it belongs in CalendarDate.