Parse, validate, manipulate, and display dates and times in JavaScript with internationalization support
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Comprehensive formatting system with customizable format strings, relative time display, calendar formatting, and localized output for displaying Moment instances in various human-readable formats.
Core formatting methods for converting Moment instances to strings with customizable format patterns.
/**
* Format the moment according to the given format string
* @param format - Format string using tokens (defaults to ISO 8601)
* @returns Formatted date string
*/
format(format?: string): string;
/**
* Get string representation (same as format() with no arguments)
* @returns ISO 8601 formatted string
*/
toString(): string;
/**
* Get ISO 8601 string representation
* @param keepOffset - Whether to keep the current UTC offset (default: false)
* @returns ISO 8601 formatted string
*/
toISOString(keepOffset?: boolean): string;
/**
* Get JSON representation (same as toISOString())
* @returns ISO 8601 formatted string for JSON serialization
*/
toJSON(): string;
/**
* Get debug representation for console.log and debugging
* @returns String representation for debugging
*/
inspect(): string;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:45.123Z");
// Basic formatting
console.log(date.format()); // "2023-12-25T15:30:45Z" (default ISO 8601)
console.log(date.toString()); // Same as format()
// Custom formats
console.log(date.format("YYYY-MM-DD")); // "2023-12-25"
console.log(date.format("MMMM Do, YYYY")); // "December 25th, 2023"
console.log(date.format("dddd, MMMM DD, YYYY h:mm A")); // "Monday, December 25, 2023 3:30 PM"
console.log(date.format("DD/MM/YYYY HH:mm:ss")); // "25/12/2023 15:30:45"
// ISO string formatting
console.log(date.toISOString()); // "2023-12-25T15:30:45.123Z"
console.log(date.toISOString(true)); // Keeps offset if not UTC
// JSON serialization
console.log(JSON.stringify({ date })); // {"date":"2023-12-25T15:30:45.123Z"}Methods for displaying time relative to other moments or the current time.
/**
* Get relative time from now
* @param withoutSuffix - Remove "ago" or "in" suffix (default: false)
* @returns Relative time string like "2 hours ago" or "in 3 days"
*/
fromNow(withoutSuffix?: boolean): string;
/**
* Get relative time to now (opposite of fromNow)
* @param withoutPrefix - Remove "in" or "ago" prefix (default: false)
* @returns Relative time string
*/
toNow(withoutPrefix?: boolean): string;
/**
* Get relative time from another moment
* @param inp - Reference moment to compare against
* @param suffix - Include "ago" or "in" suffix (default: true)
* @returns Relative time string
*/
from(inp: MomentInput, suffix?: boolean): string;
/**
* Get relative time to another moment (opposite of from)
* @param inp - Reference moment to compare against
* @param suffix - Include "ago" or "in" suffix (default: true)
* @returns Relative time string
*/
to(inp: MomentInput, suffix?: boolean): string;Usage Examples:
import moment from "moment";
const now = moment();
const past = moment().subtract(2, "hours");
const future = moment().add(3, "days");
// Relative to now
console.log(past.fromNow()); // "2 hours ago"
console.log(past.fromNow(true)); // "2 hours" (without suffix)
console.log(future.fromNow()); // "in 3 days"
console.log(past.toNow()); // "in 2 hours"
console.log(future.toNow()); // "3 days ago"
// Relative to specific moment
const reference = moment("2023-12-25");
const target = moment("2023-12-28");
console.log(target.from(reference)); // "in 3 days"
console.log(target.to(reference)); // "3 days ago"
console.log(target.from(reference, false)); // "3 days" (without suffix)Display dates in calendar format showing relationship to current week.
/**
* Get calendar time representation (relative to current week)
* @returns Calendar string like "Today at 2:30 PM", "Last Monday at 2:30 PM"
*/
calendar(): string;
/**
* Get calendar time with custom format specification
* @param formats - Custom calendar format object
* @returns Formatted calendar string
*/
calendar(formats: CalendarSpec): string;
/**
* Get calendar time relative to specific reference time
* @param time - Reference time to compare against (defaults to now)
* @param formats - Optional custom format specification
* @returns Formatted calendar string
*/
calendar(time: MomentInput, formats?: CalendarSpec): string;
interface CalendarSpec {
sameDay?: CalendarSpecVal; // Today
nextDay?: CalendarSpecVal; // Tomorrow
lastDay?: CalendarSpecVal; // Yesterday
nextWeek?: CalendarSpecVal; // Next week
lastWeek?: CalendarSpecVal; // Last week
sameElse?: CalendarSpecVal; // Everything else
[x: string]: CalendarSpecVal | void; // Custom keys
}
type CalendarSpecVal = string | ((m?: MomentInput, now?: Moment) => string);Usage Examples:
import moment from "moment";
const now = moment();
const today = moment().hour(14).minute(30);
const yesterday = moment().subtract(1, "day").hour(10);
const tomorrow = moment().add(1, "day").hour(16);
const lastWeek = moment().subtract(7, "days");
const nextMonth = moment().add(1, "month");
// Default calendar formatting
console.log(today.calendar()); // "Today at 2:30 PM"
console.log(yesterday.calendar()); // "Yesterday at 10:00 AM"
console.log(tomorrow.calendar()); // "Tomorrow at 4:00 PM"
console.log(lastWeek.calendar()); // "Last Monday at 2:30 PM"
console.log(nextMonth.calendar()); // "01/25/2024"
// Custom calendar formats
const customFormats = {
sameDay: "[Today] HH:mm",
nextDay: "[Tomorrow] HH:mm",
lastDay: "[Yesterday] HH:mm",
nextWeek: "dddd [at] HH:mm",
lastWeek: "[Last] dddd [at] HH:mm",
sameElse: "DD/MM/YYYY"
};
console.log(today.calendar(customFormats)); // "Today 14:30"
console.log(yesterday.calendar(customFormats)); // "Yesterday 10:00"
// Calendar relative to specific time
const reference = moment("2023-12-25T12:00:00");
const target = moment("2023-12-25T15:30:00");
console.log(target.calendar(reference)); // "Today at 3:30 PM"Methods for getting numeric representations of the moment.
/**
* Get Unix timestamp in milliseconds (same as +moment or moment.getTime())
* @returns Milliseconds since Unix epoch
*/
valueOf(): number;
/**
* Get Unix timestamp in seconds (not milliseconds)
* @returns Seconds since Unix epoch
*/
unix(): number;
/**
* Convert to native JavaScript Date object
* @returns Native Date object
*/
toDate(): Date;
/**
* Convert to array of date components
* @returns Array of [year, month, day, hour, minute, second, millisecond]
*/
toArray(): number[];
/**
* Convert to object with date components
* @returns Object with date/time properties
*/
toObject(): MomentObjectOutput;
interface MomentObjectOutput {
years: number;
months: number; // 0-indexed (0 = January)
date: number; // Day of month
hours: number;
minutes: number;
seconds: number;
milliseconds: number;
}Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:45.123Z");
// Numeric representations
console.log(date.valueOf()); // 1703517045123 (milliseconds)
console.log(+date); // Same as valueOf()
console.log(date.unix()); // 1703517045 (seconds)
// Conversions
console.log(date.toDate()); // Native Date object
console.log(date.toArray()); // [2023, 11, 25, 15, 30, 45, 123]
const obj = date.toObject();
console.log(obj);
// {
// years: 2023,
// months: 11, // December (0-indexed)
// date: 25,
// hours: 15,
// minutes: 30,
// seconds: 45,
// milliseconds: 123
// }Formatting with locale-specific patterns and display names.
/**
* Get or set locale for this moment instance
* @param locale - Locale identifier (when setting)
* @returns Current locale string (when getting) or this moment (when setting)
*/
locale(): string;
locale(locale: LocaleSpecifier): Moment;
/**
* Get locale data object for this moment's locale
* @returns Locale data object with formatting functions and display names
*/
localeData(): Locale;
type LocaleSpecifier = string | Moment | Duration | string[] | boolean;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:00");
// Default locale formatting
console.log(date.format("LLLL")); // "Monday, December 25, 2023 3:30 PM"
console.log(date.format("dddd, MMMM Do YYYY")); // "Monday, December 25th 2023"
// Set locale for instance
const frenchDate = date.clone().locale("fr");
console.log(frenchDate.format("LLLL")); // "lundi 25 décembre 2023 15:30"
console.log(frenchDate.format("dddd, MMMM Do YYYY")); // "lundi, décembre 25e 2023"
// Multiple locales
const germanDate = date.clone().locale("de");
console.log(germanDate.format("LLLL")); // "Montag, 25. Dezember 2023 15:30"
// Get current locale
console.log(date.locale()); // "en"
console.log(frenchDate.locale()); // "fr"
// Locale data
const localeData = frenchDate.localeData();
console.log(localeData.months()); // French month names array// Year
YYYY // 2023
YY // 23
// Month
MM // 01-12
MMM // Jan, Feb, ..., Dec
MMMM // January, February, ..., December
M // 1-12
// Day of Month
DD // 01-31
Do // 1st, 2nd, 3rd, ..., 31st
D // 1-31
// Day of Year
DDDD // 001-365
DDD // 1-365
// Day of Week
dddd // Monday, Tuesday, ..., Sunday
ddd // Mon, Tue, ..., Sun
dd // Mo, Tu, ..., Su
d // 0-6 (Sunday = 0)
e // 0-6 (locale aware)
E // 1-7 (ISO, Monday = 1)// Hour
HH // 00-23
H // 0-23
hh // 01-12
h // 1-12
kk // 01-24
k // 1-24
// Minute
mm // 00-59
m // 0-59
// Second
ss // 00-59
s // 0-59
// Fractional Second
SSS // 000-999 (3 digits)
SS // 00-99 (2 digits)
S // 0-9 (1 digit)
// AM/PM
A // AM, PM
a // am, pm
// Timezone
Z // +05:00, -08:00
ZZ // +0500, -0800// Week of Year
ww // 01-53 (locale aware)
w // 1-53 (locale aware)
WW // 01-53 (ISO week)
W // 1-53 (ISO week)
// Week Year
gggg // Week year (locale aware)
gg // 2-digit week year (locale aware)
GGGG // ISO week year
GG // 2-digit ISO week year// Locale-aware formatting (requires locale to be set)
LTS // 8:30:25 PM
LT // 8:30 PM
L // 09/04/1986
LL // September 4, 1986
LLL // September 4, 1986 8:30 PM
LLLL // Monday, September 4, 1986 8:30 PM
// Lowercase versions
lts // 8:30:25 pm
lt // 8:30 pm
l // 9/4/1986
ll // Sep 4, 1986
lll // Sep 4, 1986 8:30 pm
llll // Mon, Sep 4, 1986 8:30 pm// Escape characters in square brackets
"[Today is] dddd" // "Today is Monday"
"YYYY [escaped] YYYY" // "2023 escaped 2023"
"[Quarter] Q, YYYY" // "Quarter 4, 2023"Format Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:45.123");
// Common formats
console.log(date.format("YYYY-MM-DD")); // "2023-12-25"
console.log(date.format("MM/DD/YYYY")); // "12/25/2023"
console.log(date.format("DD-MMM-YYYY")); // "25-Dec-2023"
console.log(date.format("dddd, MMMM Do, YYYY")); // "Monday, December 25th, 2023"
// Time formats
console.log(date.format("HH:mm:ss")); // "15:30:45"
console.log(date.format("h:mm A")); // "3:30 PM"
console.log(date.format("hh:mm:ss A")); // "03:30:45 PM"
// Combined formats
console.log(date.format("YYYY-MM-DD HH:mm:ss")); // "2023-12-25 15:30:45"
console.log(date.format("ddd, MMM Do YYYY, h:mm A")); // "Mon, Dec 25th 2023, 3:30 PM"
// With literals
console.log(date.format("[Today is] dddd [the] Do [of] MMMM"));
// "Today is Monday the 25th of December"