This capability provides comprehensive methods for creating Moment date objects from various input sources and converting between different date representations.
constructor(options?: {
formats?: Partial<DateIOFormats>;
locale?: string;
instance?: any;
})Creates a new MomentUtils instance with optional configuration.
Parameters:
options.formats - Custom format strings to override defaultsoptions.locale - Locale code for localized formatting (e.g., "en", "fr", "de")options.instance - Custom moment.js instance to use instead of defaultUsage:
// Default instance
const utils = new MomentUtils();
// With locale
const utils = new MomentUtils({ locale: "fr" });
// With custom formats
const utils = new MomentUtils({
formats: {
fullDate: "DD/MM/YYYY",
shortDate: "DD/MM"
}
});date<
TArg extends unknown = undefined,
TRes extends unknown = TArg extends null
? null
: TArg extends undefined
? defaultMoment.Moment
: defaultMoment.Moment | null
>(value?: TArg): TResCreates a Moment date object from various input types. Supports generic type inference for null handling.
Parameters:
value - Input value (ISO string, Date object, timestamp, or undefined for current date)Returns: Moment object, null if input is null, or Moment | null if input could be invalid
Usage:
const now = utils.date(); // Current date/time
const fromISO = utils.date("2023-10-30T14:30:00.000Z");
const fromDate = utils.date(new Date());
const fromTimestamp = utils.date(1698672600000);
const nullable = utils.date(null); // Returns nullparseISO(isoString: string): MomentParses an ISO 8601 date string into a Moment object with strict validation.
Parameters:
isoString - ISO 8601 formatted date stringReturns: Moment object
Usage:
const date = utils.parseISO("2023-10-30T14:30:00.000Z");
const dateOnly = utils.parseISO("2023-10-30");parse(value: string, format: string): Moment | nullParses a date string using a custom format pattern. Returns null for empty strings or invalid dates.
Parameters:
value - Date string to parseformat - Moment.js format string (e.g., "YYYY-MM-DD", "DD/MM/YYYY HH:mm")Returns: Moment object or null if parsing fails
Usage:
const date1 = utils.parse("30/10/2023", "DD/MM/YYYY");
const date2 = utils.parse("2023-10-30 14:30", "YYYY-MM-DD HH:mm");
const invalid = utils.parse("", "YYYY-MM-DD"); // Returns null
const withLocale = utils.parse("30 octobre 2023", "DD MMMM YYYY"); // If locale is "fr"toISO(value: Moment): stringConverts a Moment object to an ISO 8601 string representation.
Parameters:
value - Moment object to convertReturns: ISO 8601 formatted string
Usage:
const moment = utils.date("2023-10-30T14:30:00.000Z");
const isoString = utils.toISO(moment); // "2023-10-30T14:30:00.000Z"toJsDate(value: Moment): DateConverts a Moment object to a native JavaScript Date object.
Parameters:
value - Moment object to convertReturns: JavaScript Date object
Usage:
const moment = utils.date("2023-10-30T14:30:00.000Z");
const jsDate = utils.toJsDate(moment);
console.log(jsDate instanceof Date); // truelib: string; // Always "moment"
moment: typeof import("moment"); // Moment.js instance
locale?: string; // Current locale code
formats: DateIOFormats; // Current format configurationThe utility exposes several read-only properties for inspection:
Usage:
const utils = new MomentUtils({ locale: "fr" });
console.log(utils.lib); // "moment"
console.log(utils.locale); // "fr"
console.log(utils.formats.fullDate); // "ll" (or custom if overridden)