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
Dual-purpose methods for reading and writing specific date/time components with support for all time units. These methods can both retrieve current values and set new values depending on whether arguments are provided.
Get or set the year component of the moment.
/**
* Get the year (4 digits)
* @returns Year as number (e.g., 2023)
*/
year(): number;
/**
* Set the year
* @param y - Year to set (4 digit or 2 digit)
* @returns This moment instance (for chaining)
*/
year(y: number): Moment;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:00");
// Get year
console.log(date.year()); // 2023
// Set year
date.year(2024);
console.log(date.format()); // "2024-12-25T15:30:00"
// Chain with other operations
const newYear = moment().year(2025).month(0).date(1); // January 1, 2025Get or set the quarter of the year (1-4).
/**
* Get the quarter (1-4)
* @returns Quarter as number
*/
quarter(): number;
/**
* Set the quarter
* @param q - Quarter to set (1-4)
* @returns This moment instance (for chaining)
*/
quarter(q: number): Moment;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:00");
// Get quarter
console.log(date.quarter()); // 4 (December is Q4)
// Set quarter (adjusts month to first month of quarter)
date.quarter(2);
console.log(date.format()); // "2023-04-25T15:30:00" (Q2 starts with April)
// Quarter boundaries
console.log(moment("2023-01-15").quarter()); // 1
console.log(moment("2023-04-15").quarter()); // 2
console.log(moment("2023-07-15").quarter()); // 3
console.log(moment("2023-10-15").quarter()); // 4Get or set the month component (0-11 for numbers, or month names for strings).
/**
* Get the month (0-11, where 0 = January)
* @returns Month as number
*/
month(): number;
/**
* Set the month
* @param M - Month to set (0-11 as number, or month name as string)
* @returns This moment instance (for chaining)
*/
month(M: number|string): Moment;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:00");
// Get month
console.log(date.month()); // 11 (December is 11, zero-indexed)
// Set month by number (0-indexed)
date.month(0); // January
console.log(date.format()); // "2023-01-25T15:30:00"
// Set month by name
date.month("March");
console.log(date.format()); // "2023-03-25T15:30:00"
// Short month names also work
date.month("Dec");
console.log(date.format()); // "2023-12-25T15:30:00"
// Handle month overflow (adjusts to last day of month if needed)
const jan31 = moment("2023-01-31");
jan31.month(1); // February doesn't have 31 days
console.log(jan31.format()); // "2023-02-28T00:00:00" (adjusted to last day)Get or set the day of the month (1-31).
/**
* Get the day of the month (1-31)
* @returns Day as number
*/
date(): number;
/**
* Set the day of the month
* @param d - Day to set (1-31)
* @returns This moment instance (for chaining)
*/
date(d: number): Moment;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:00");
// Get date
console.log(date.date()); // 25
// Set date
date.date(15);
console.log(date.format()); // "2023-12-15T15:30:00"
// Handle overflow (wraps to next month)
date.date(35);
console.log(date.format()); // "2024-01-04T15:30:00" (35 - 31 = 4 days into January)
// Set to last day of month
const lastDay = moment("2023-02-01").date(28);
console.log(lastDay.format()); // "2023-02-28T00:00:00"Get or set the day of the week (0-6 where Sunday = 0, or day names).
/**
* Get the day of the week (0-6, where 0 = Sunday)
* @returns Day of week as number
*/
day(): number;
/**
* Set the day of the week
* @param d - Day to set (0-6 as number, or day name as string)
* @returns This moment instance (for chaining)
*/
day(d: number|string): Moment;
/**
* Get locale-aware weekday (0-6, where first day depends on locale)
* @returns Weekday as number
*/
weekday(): number;
/**
* Set locale-aware weekday
* @param d - Weekday to set (0-6)
* @returns This moment instance (for chaining)
*/
weekday(d: number): Moment;
/**
* Get ISO weekday (1-7, where 1 = Monday)
* @returns ISO weekday as number
*/
isoWeekday(): number;
/**
* Set ISO weekday
* @param d - ISO weekday to set (1-7 as number, or day name as string)
* @returns This moment instance (for chaining)
*/
isoWeekday(d: number|string): Moment;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:00"); // Monday
// Get day of week
console.log(date.day()); // 1 (Monday, Sunday = 0)
console.log(date.weekday()); // Locale-dependent
console.log(date.isoWeekday()); // 1 (Monday, ISO standard)
// Set by number
date.day(5); // Friday
console.log(date.format()); // "2023-12-29T15:30:00"
// Set by name
date.day("Wednesday");
console.log(date.format()); // "2023-12-27T15:30:00"
// ISO weekday (Monday = 1)
date.isoWeekday(7); // Sunday
console.log(date.format()); // "2023-12-31T15:30:00"
// Weekday names work with isoWeekday too
date.isoWeekday("Monday");
console.log(date.format()); // "2023-12-25T15:30:00"Get or set the hour component (0-23).
/**
* Get the hour (0-23)
* @returns Hour as number
*/
hour(): number;
/**
* Set the hour
* @param h - Hour to set (0-23)
* @returns This moment instance (for chaining)
*/
hour(h: number): Moment;
// Alias methods
hours(): number;
hours(h: number): Moment;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:00");
// Get hour
console.log(date.hour()); // 15
console.log(date.hours()); // 15 (alias)
// Set hour
date.hour(9);
console.log(date.format()); // "2023-12-25T09:30:00"
// 24-hour format
date.hour(23);
console.log(date.format()); // "2023-12-25T23:30:00"
// Handle overflow (wraps to next day)
date.hour(25);
console.log(date.format()); // "2023-12-26T01:30:00"Get or set the minute component (0-59).
/**
* Get the minute (0-59)
* @returns Minute as number
*/
minute(): number;
/**
* Set the minute
* @param m - Minute to set (0-59)
* @returns This moment instance (for chaining)
*/
minute(m: number): Moment;
// Alias methods
minutes(): number;
minutes(m: number): Moment;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:00");
// Get minute
console.log(date.minute()); // 30
console.log(date.minutes()); // 30 (alias)
// Set minute
date.minute(45);
console.log(date.format()); // "2023-12-25T15:45:00"
// Handle overflow (wraps to next hour)
date.minute(75);
console.log(date.format()); // "2023-12-25T16:15:00"Get or set the second component (0-59).
/**
* Get the second (0-59)
* @returns Second as number
*/
second(): number;
/**
* Set the second
* @param s - Second to set (0-59)
* @returns This moment instance (for chaining)
*/
second(s: number): Moment;
// Alias methods
seconds(): number;
seconds(s: number): Moment;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:45");
// Get second
console.log(date.second()); // 45
console.log(date.seconds()); // 45 (alias)
// Set second
date.second(30);
console.log(date.format()); // "2023-12-25T15:30:30"
// Handle overflow (wraps to next minute)
date.second(75);
console.log(date.format()); // "2023-12-25T15:31:15"Get or set the millisecond component (0-999).
/**
* Get the millisecond (0-999)
* @returns Millisecond as number
*/
millisecond(): number;
/**
* Set the millisecond
* @param ms - Millisecond to set (0-999)
* @returns This moment instance (for chaining)
*/
millisecond(ms: number): Moment;
// Alias methods
milliseconds(): number;
milliseconds(ms: number): Moment;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:45.123");
// Get millisecond
console.log(date.millisecond()); // 123
console.log(date.milliseconds()); // 123 (alias)
// Set millisecond
date.millisecond(500);
console.log(date.format("YYYY-MM-DD HH:mm:ss.SSS")); // "2023-12-25 15:30:45.500"
// Handle overflow (wraps to next second)
date.millisecond(1500);
console.log(date.format("YYYY-MM-DD HH:mm:ss.SSS")); // "2023-12-25 15:30:46.500"Get or set week-related values.
/**
* Get the week of the year (1-53, locale-aware)
* @returns Week number
*/
week(): number;
/**
* Set the week of the year
* @param d - Week number to set (1-53)
* @returns This moment instance (for chaining)
*/
week(d: number): Moment;
/**
* Get the ISO week of the year (1-53)
* @returns ISO week number
*/
isoWeek(): number;
/**
* Set the ISO week of the year
* @param d - ISO week number to set (1-53)
* @returns This moment instance (for chaining)
*/
isoWeek(d: number): Moment;
/**
* Get the week year (locale-aware)
* @returns Week year as number
*/
weekYear(): number;
/**
* Set the week year
* @param d - Week year to set
* @returns This moment instance (for chaining)
*/
weekYear(d: number): Moment;
/**
* Get the ISO week year
* @returns ISO week year as number
*/
isoWeekYear(): number;
/**
* Set the ISO week year
* @param d - ISO week year to set
* @returns This moment instance (for chaining)
*/
isoWeekYear(d: number): Moment;
/**
* Get the day of the year (1-366)
* @returns Day of year as number
*/
dayOfYear(): number;
/**
* Set the day of the year
* @param d - Day of year to set (1-366)
* @returns This moment instance (for chaining)
*/
dayOfYear(d: number): Moment;
// Alias methods
weeks(): number;
weeks(d: number): Moment;
isoWeeks(): number;
isoWeeks(d: number): Moment;Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:00");
// Week operations
console.log(date.week()); // Week number (locale-dependent)
console.log(date.isoWeek()); // 52 (ISO week)
console.log(date.weekYear()); // 2023
console.log(date.isoWeekYear()); // 2023
// Day of year
console.log(date.dayOfYear()); // 359 (December 25 is the 359th day)
// Set week
date.week(1); // First week of year
console.log(date.format()); // Date adjusted to first week
// Set day of year
date.dayOfYear(100); // 100th day of year
console.log(date.format()); // "2023-04-10T15:30:00" (April 10)
// ISO week operations
const isoDate = moment("2023-01-01");
console.log(isoDate.isoWeek()); // ISO week number
isoDate.isoWeek(26); // Set to week 26
console.log(isoDate.format()); // Date adjusted to week 26Generic methods for getting and setting any time unit.
/**
* Get value for any time unit
* @param unit - Time unit to get
* @returns Value for the specified unit
*/
get(unit: unitOfTime.All): number;
/**
* Set value for any time unit
* @param unit - Time unit to set
* @param value - Value to set
* @returns This moment instance (for chaining)
*/
set(unit: unitOfTime.All, value: number): Moment;
/**
* Set multiple time units at once
* @param objectLiteral - Object containing unit-value pairs
* @returns This moment instance (for chaining)
*/
set(objectLiteral: MomentSetObject): Moment;
interface MomentSetObject extends MomentInputObject {
weekYears?: numberlike;
weekYear?: numberlike;
gg?: numberlike;
isoWeekYears?: numberlike;
isoWeekYear?: numberlike;
GG?: numberlike;
quarters?: numberlike;
quarter?: numberlike;
Q?: numberlike;
weeks?: numberlike;
week?: numberlike;
w?: numberlike;
isoWeeks?: numberlike;
isoWeek?: numberlike;
W?: numberlike;
dayOfYears?: numberlike;
dayOfYear?: numberlike;
DDD?: numberlike;
weekdays?: numberlike;
weekday?: numberlike;
e?: numberlike;
isoWeekdays?: numberlike;
isoWeekday?: numberlike;
E?: numberlike;
}Usage Examples:
import moment from "moment";
const date = moment("2023-12-25T15:30:45.123");
// Generic get operations
console.log(date.get("year")); // 2023
console.log(date.get("month")); // 11 (December)
console.log(date.get("date")); // 25
console.log(date.get("hour")); // 15
console.log(date.get("minute")); // 30
console.log(date.get("second")); // 45
console.log(date.get("millisecond")); // 123
// Alternative unit names
console.log(date.get("y")); // 2023 (year)
console.log(date.get("M")); // 11 (month)
console.log(date.get("d")); // 25 (date)
console.log(date.get("h")); // 15 (hour)
console.log(date.get("m")); // 30 (minute)
console.log(date.get("s")); // 45 (second)
console.log(date.get("ms")); // 123 (millisecond)
// Generic set operations
date.set("year", 2024);
date.set("month", 5); // June (0-indexed)
date.set("date", 15);
console.log(date.format()); // "2024-06-15T15:30:45"
// Set multiple values at once
date.set({
year: 2025,
month: 11, // December
date: 31,
hour: 23,
minute: 59,
second: 59
});
console.log(date.format()); // "2025-12-31T23:59:59"
// Use shorter unit names
date.set({
y: 2023,
M: 0, // January
d: 1,
h: 0,
m: 0,
s: 0,
ms: 0
});
console.log(date.format()); // "2023-01-01T00:00:00"
// Week-based setting
date.set("week", 26);
date.set("weekday", 1); // Monday
console.log(date.format()); // Date adjusted to Monday of week 26
// Day of year setting
date.set("dayOfYear", 182); // Middle of year
console.log(date.format()); // Date adjusted to day 182All setter methods return the Moment instance, allowing for method chaining.
import moment from "moment";
// Chain multiple setters
const customDate = moment()
.year(2024)
.month(5) // June
.date(15)
.hour(14)
.minute(30)
.second(0)
.millisecond(0);
console.log(customDate.format()); // "2024-06-15T14:30:00"
// Mix with other operations
const result = moment("2023-01-01")
.month(11) // December
.date(25) // Christmas
.hour(18) // 6 PM
.add(1, "year") // 2024
.startOf("day") // Beginning of day
.add(12, "hours"); // Noon
console.log(result.format()); // "2024-12-25T12:00:00"
// Complex chaining example
const complexDate = moment()
.set({
year: 2023,
month: 0, // January
date: 1
})
.dayOfYear(100) // 100th day
.hour(15)
.minute(30)
.startOf("minute"); // Reset seconds/milliseconds
console.log(complexDate.format()); // Date representing 100th day at 15:30:00