Guides / Working with time
Months and days
Store a month without a day, or a recurring birthday without a year.
In plain JavaScript, storing a card expiry means sticking a fake 1st on the end. Storing a birthday without a birth year means picking a dummy leap year like 2000. YearMonth and MonthDay store just the parts that actually exist.
Billing months and card expiries
Use yearMonth() for ISO strings or year-and-month numbers, and monthYear() for card expiries like 11/28:
import { monthYear, yearMonth } from 'native-dates';
const billing = yearMonth('2026-11');
const expiry = monthYear('11/28');
billing.daysInMonth; // 30
billing.quarter; // 4
expiry.isPast('UTC'); // falseCheck whether a date falls inside a month with contains(), step forward by months or quarters with plus(), and pick a specific day with on():
billing.contains('2026-11-16'); // true
billing.plus(3, 'months').toString(); // '2027-02'
yearMonth('2026-02').on(31).toString(); // '2026-02-28'
yearMonth('2028-02').on(31).toString(); // '2028-02-29'Day 31 lands on the last day of shorter months automatically, including February 29 in leap years.
Birthdays and annual holidays
Use monthDay() for month-first strings and dayMonth() for day-first strings. The function name sets the order, so 05/10 means the same thing everywhere:
import { dayMonth, monthDay } from 'native-dates';
const birthday = monthDay('10/17');
const european = dayMonth('17/10');
birthday.equals(european); // true
birthday.in(2027).toString(); // '2027-10-17'Find the next or most recent occurrence from today or any reference date with next() and previous():
birthday.next('2026-05-01').toString(); // '2026-10-17'
birthday.next('2026-10-17').toString(); // '2027-10-17'
birthday.previous('2026-10-17').toString(); // '2025-10-17'February 29 shifts to February 28 in non-leap years. Call fallsOn() to check whether a given date matches:
const leapDay = monthDay(2, 29);
leapDay.in(2027).toString(); // '2027-02-28'
leapDay.fallsOn('2027-02-28'); // true
leapDay.fallsOn('2028-02-28'); // false
leapDay.fallsOn('2028-02-29'); // trueMoving between months, days, and full dates
Read .yearMonth or .monthDay off any date or moment, or call as() mid-chain:
import { date, datetime } from 'native-dates';
const invoice = date('2026-11-16');
invoice.yearMonth.toString(); // '2026-11'
invoice.monthDay.toString(); // '11-16'
datetime('2026-11-01T02:00:00Z', 'America/Los_Angeles').as('yearMonth').toString();
// '2026-10'Going the other way, call on() with a day on a month or in() with a year on a birthday to get a full calendar date.
Formatting and storing
Both types format through .text and format(), and turn into ISO strings with toString():
import { monthDay, yearMonth } from 'native-dates';
yearMonth('2026-11').format('long', 'en-US'); // 'November 2026'
yearMonth('2026-05').format('long', 'ru'); // 'май 2026 г.'
monthDay('10-17').format('long', 'en-US'); // 'October 17'
yearMonth('2026-11').toString(); // '2026-11'
monthDay('10/17').toString(); // '10-17'Strings like 2026-11 and 10-17 store cleanly in a database or URL and parse straight back through yearMonth() and monthDay().