TypeScript adapter providing a unified interface for the Moment.js date library through the date-io abstraction layer.
npx @tessl/cli install tessl/npm-date-io--moment@3.2.0@date-io/moment is a TypeScript adapter that provides a unified interface for the Moment.js date management library. It abstracts Moment.js operations through a standardized API defined by the @date-io/core interface, enabling developers to build date/time UI components that can work interchangeably with different underlying date libraries (moment, date-fns, dayjs, luxon).
npm install @date-io/moment momentimport MomentUtils from "@date-io/moment";For CommonJS:
const MomentUtils = require("@date-io/moment");import MomentUtils from "@date-io/moment";
// Initialize the utility with optional locale and custom formats
const utils = new MomentUtils({ locale: "en" });
// Create dates
const now = utils.date(); // Current date
const specificDate = utils.date("2023-10-30T14:30:00.000Z");
const parsedDate = utils.parse("2023-10-30", "YYYY-MM-DD");
// Format dates
const formatted = utils.format(now, "fullDate"); // "Oct 30, 2023"
const customFormat = utils.formatByString(now, "YYYY-MM-DD HH:mm");
// Date operations
const tomorrow = utils.addDays(now, 1);
const startOfMonth = utils.startOfMonth(now);
const isValid = utils.isValid(specificDate);@date-io/moment is built around several key components:
Core functionality for creating and converting date objects between different formats and representations.
class MomentUtils {
constructor(options?: {
formats?: Partial<DateIOFormats>;
locale?: string;
instance?: any;
});
date<TArg, TRes>(value?: TArg): TRes;
parseISO(isoString: string): Moment;
parse(value: string, format: string): Moment | null;
toISO(value: Moment): string;
toJsDate(value: Moment): Date;
}Methods for validating dates and comparing date relationships including equality, ordering, and range checking.
interface DateValidation {
isValid(value: any): boolean;
isNull(date: Moment): boolean;
isEqual(value: any, comparing: any): boolean;
isAfter(date: Moment, value: Moment): boolean;
isBefore(date: Moment, value: Moment): boolean;
isWithinRange(date: Moment, range: [Moment, Moment]): boolean;
}Date Validation and Comparison
Comprehensive date arithmetic operations for adding/subtracting time units and navigating between date boundaries.
interface DateManipulation {
addSeconds(date: Moment, count: number): Moment;
addMinutes(date: Moment, count: number): Moment;
addHours(date: Moment, count: number): Moment;
addDays(date: Moment, count: number): Moment;
addWeeks(date: Moment, count: number): Moment;
addMonths(date: Moment, count: number): Moment;
addYears(date: Moment, count: number): Moment;
startOfDay(date: Moment): Moment;
endOfDay(date: Moment): Moment;
}Date formatting capabilities with predefined format keys and custom format strings, supporting localized output.
interface DateFormatting {
format(date: Moment, formatKey: keyof DateIOFormats): string;
formatByString(date: Moment, formatString: string): string;
formatNumber(numberToFormat: string): string;
getFormatHelperText(format: string): string;
}
interface DateIOFormats {
fullDate: string;
fullDateTime: string;
shortDate: string;
year: string;
month: string;
keyboardDate: string;
// ... and 20+ more predefined formats
}Getter and setter methods for accessing and modifying individual date components (year, month, day, hour, etc.).
interface DateComponents {
getYear(date: Moment): number;
setYear(date: Moment, year: number): Moment;
getMonth(date: Moment): number;
setMonth(date: Moment, count: number): Moment;
getDate(date: Moment): number;
setDate(date: Moment, date: number): Moment;
getHours(date: Moment): number;
setHours(date: Moment, count: number): Moment;
}Advanced calendar operations for working with date ranges, week arrays, month arrays, and calendar views.
interface CalendarOperations {
getWeekArray(date: Moment): Moment[][];
getMonthArray(date: Moment): Moment[];
getYearRange(start: Moment, end: Moment): Moment[];
getWeekdays(): string[];
mergeDateAndTime(date: Moment, time: Moment): Moment;
}Comprehensive localization functionality including locale detection, meridiem text, and format helpers.
interface Localization {
getCurrentLocaleCode(): string;
is12HourCycleInCurrentLocale(): boolean;
getFormatHelperText(format: string): string;
getMeridiemText(ampm: "am" | "pm"): string;
}type Unit =
| "years"
| "quarters"
| "months"
| "weeks"
| "days"
| "hours"
| "minutes"
| "seconds"
| "milliseconds";
type Moment = import("moment").Moment;
interface DateIOFormats<TLibFormatToken = string> {
fullDate: TLibFormatToken;
fullDateWithWeekday: TLibFormatToken;
normalDate: TLibFormatToken;
normalDateWithWeekday: TLibFormatToken;
shortDate: TLibFormatToken;
year: TLibFormatToken;
month: TLibFormatToken;
monthShort: TLibFormatToken;
monthAndYear: TLibFormatToken;
monthAndDate: TLibFormatToken;
weekday: TLibFormatToken;
weekdayShort: TLibFormatToken;
dayOfMonth: TLibFormatToken;
hours12h: TLibFormatToken;
hours24h: TLibFormatToken;
minutes: TLibFormatToken;
seconds: TLibFormatToken;
fullTime: TLibFormatToken;
fullTime12h: TLibFormatToken;
fullTime24h: TLibFormatToken;
fullDateTime: TLibFormatToken;
fullDateTime12h: TLibFormatToken;
fullDateTime24h: TLibFormatToken;
keyboardDate: TLibFormatToken;
keyboardDateTime: TLibFormatToken;
keyboardDateTime12h: TLibFormatToken;
keyboardDateTime24h: TLibFormatToken;
}