@esoh/ymd
v2.2.0
Published
Year-Month-Day class
Maintainers
Readme
@esoh/ymd
A TypeScript utility class for working with dates in YYYY-MM-DD format.
Install
npm install @esoh/ymdor if you're using yarn:
yarn add @esoh/ymdUsage
import { Ymd, DayOfWeek } from '@esoh/ymd';
// Create from string
const date = new Ymd('2023-12-25');
date.value; // '2023-12-25'
// Create from Date objects
const today = Ymd.todayAtLocalTimezone();
const utcToday = Ymd.todayAtUtc();
const tokyoToday = Ymd.todayAtTimezone('Asia/Tokyo');
const dateInTokyo = Ymd.fromDateAtTimezone(someDate, 'Asia/Tokyo');
// Date arithmetic
const tomorrow = date.addDays(1);
const nextMonth = date.addMonths(1);
// Comparisons
date.isToday(); // boolean (uses local timezone)
date.isToday('America/New_York'); // boolean (uses specified timezone)
date.isInTheFuture(); // boolean
date.gt('2023-12-24'); // boolean
// Week operations
const monday = date.previousDayOfWeekOccurrence(DayOfWeek.MONDAY);
const weekStart = date.startOfWeek(DayOfWeek.MONDAY);
// Formatting
date.format('MMM dd, yyyy'); // "Dec 25, 2023"
// Date ranges
const days = Ymd.dayArray({ from: '2023-12-01', to: '2023-12-31' });
const monthRange = date.calendarMonthDateRange({ weekStartsOnWeekDayIdx: DayOfWeek.MONDAY });Key Features
- Timezone-aware: Support for local, UTC, and specific timezone dates
- Date arithmetic: Add days/months with proper DST handling
- Week operations: Find previous/next occurrences of weekdays
- Calendar utilities: Generate month ranges and calendar weeks
- Comparisons: Rich comparison methods for date logic
- Formatting: Flexible date formatting with date-fns
API Reference
Instance Methods
Date Creation & Conversion
constructor(value: string)- Create from YYYY-MM-DD stringvalue- Returns the date as a YYYY-MM-DD stringasDateAtLocal()- Creates a JavaScript Date representing midnight at the local timeasDateAtUtc()- Creates a JavaScript Date representing midnight at the UTC timezoneasDateAtTimezone(timezone: string)- Creates a JavaScript Date representing midnight at the specified timezoneformat(formatStr: string)- Format using date-fns patterns
Date Arithmetic
addDays(count: number)- Add days with DST handlingaddMonths(count: number)- Add months (handles month overflow)
Week Operations
previousDayOfWeekOccurrence(dayOfWeek)- Last occurrence before current datecurrentOrPreviousDayOfWeekOccurrence(dayOfWeek)- Current or previous occurrencenextDayOfWeekOccurrence(dayOfWeek)- Next occurrence after current datecurrentOrNextDayOfWeekOccurrence(dayOfWeek)- Current or next occurrencestartOfWeek(weekStartsOnWeekDayIdx)- Start of week
Month Operations
startOfMonth()- First day of monthendOfMonth()- Last day of month
Comparisons
gt(other)- Greater thangte(other)- Greater than or equallt(other)- Less thanlte(other)- Less than or equaleq(other)- EqualisLaterThan(other)- Alias for gtisEarlierThan(other)- Alias for lt
Boolean Checks
isToday(timezone?)- Is today's date (optional timezone, defaults to local)isTomorrow(timezone?)- Is tomorrow's dateisYesterday(timezone?)- Is yesterday's dateisInTheFuture(timezone?)- Is in the futureisInThePast(timezone?)- Is in the pastisFutureDate(timezone?)- Alias for isInTheFutureisPastDate(timezone?)- Alias for isInThePast
Date Differences
daysSince(other)- Days since other datedaysUntil(other)- Days until other date
Calendar Utilities
calendarMonthDateRange(options)- Full month range including paddingcalendarWeeksForMonth(options)- Array of weeks for month
Static Methods
Creation
Ymd.isValid(value: string)- Validate YYYY-MM-DD formatYmd.fromDateAtLocal(date: Date)- Create from Date using local timezoneYmd.fromDateAtUtc(date: Date)- Create from Date using UTCYmd.fromDateAtTimezone(date: Date, timezone: string)- Create from Date using specific timezoneYmd.todayAtLocalTimezone()- Today in local timezoneYmd.todayAtTimezone(timezone: string)- Today in specific timezoneYmd.todayAtUtc()- Today in UTCYmd.build(year, monthZeroIndexed, dayOfMonth)- Create from components
Date Ranges
Ymd.dayArray(fromTo)- Array of all dates in rangeYmd.dayGenerator(fromTo)- Generator for dates in rangeYmd.countDaysInclusive(fromTo)- Count days in range
Comparisons
Ymd.compareAsc(a, b)- Compare ascendingYmd.compareDesc(a, b)- Compare descending
Differences
Ymd.differenceInDays(fromTo)- Days between datesYmd.differenceInMonths(fromTo)- Months between dates
Constants
DayOfWeek- Enum with SUNDAY=0, MONDAY=1, etc.
Dependencies
date-fns(peer dependency)date-fns-tz(peer dependency)typescript(peer dependency)
