This capability provides comprehensive date arithmetic operations for adding/subtracting time units and navigating between date boundaries. All operations return new Moment instances, preserving immutability.
addSeconds(date: Moment, count: number): MomentAdds or subtracts seconds from a date.
Parameters:
date - Source Moment objectcount - Number of seconds to add (positive) or subtract (negative)Returns: New Moment object with seconds added/subtracted
Usage:
const now = utils.date();
const in30Seconds = utils.addSeconds(now, 30);
const minus10Seconds = utils.addSeconds(now, -10);addMinutes(date: Moment, count: number): MomentAdds or subtracts minutes from a date.
Parameters:
date - Source Moment objectcount - Number of minutes to add (positive) or subtract (negative)Returns: New Moment object with minutes added/subtracted
Usage:
const now = utils.date();
const in15Minutes = utils.addMinutes(now, 15);
const minus30Minutes = utils.addMinutes(now, -30);addHours(date: Moment, count: number): MomentAdds or subtracts hours from a date.
Parameters:
date - Source Moment objectcount - Number of hours to add (positive) or subtract (negative)Returns: New Moment object with hours added/subtracted
Usage:
const now = utils.date();
const in5Hours = utils.addHours(now, 5);
const yesterday = utils.addHours(now, -24);addDays(date: Moment, count: number): MomentAdds or subtracts days from a date.
Parameters:
date - Source Moment objectcount - Number of days to add (positive) or subtract (negative)Returns: New Moment object with days added/subtracted
Usage:
const today = utils.date();
const tomorrow = utils.addDays(today, 1);
const lastWeek = utils.addDays(today, -7);addWeeks(date: Moment, count: number): MomentAdds or subtracts weeks from a date.
Parameters:
date - Source Moment objectcount - Number of weeks to add (positive) or subtract (negative)Returns: New Moment object with weeks added/subtracted
Usage:
const today = utils.date();
const nextWeek = utils.addWeeks(today, 1);
const twoWeeksAgo = utils.addWeeks(today, -2);addMonths(date: Moment, count: number): MomentAdds or subtracts months from a date. Handles month boundaries intelligently (e.g., Jan 31 + 1 month = Feb 28/29).
Parameters:
date - Source Moment objectcount - Number of months to add (positive) or subtract (negative)Returns: New Moment object with months added/subtracted
Usage:
const today = utils.date();
const nextMonth = utils.addMonths(today, 1);
const sixMonthsAgo = utils.addMonths(today, -6);
// Handles edge cases gracefully
const jan31 = utils.parseISO("2023-01-31T12:00:00.000Z");
const endOfFeb = utils.addMonths(jan31, 1); // Feb 28, 2023addYears(date: Moment, count: number): MomentAdds or subtracts years from a date. Handles leap years appropriately.
Parameters:
date - Source Moment objectcount - Number of years to add (positive) or subtract (negative)Returns: New Moment object with years added/subtracted
Usage:
const today = utils.date();
const nextYear = utils.addYears(today, 1);
const decade = utils.addYears(today, 10);
const fiveYearsAgo = utils.addYears(today, -5);
// Handles leap year edge cases
const feb29 = utils.parseISO("2020-02-29T12:00:00.000Z"); // Leap year
const nextYear = utils.addYears(feb29, 1); // Feb 28, 2021startOfDay(date: Moment): Moment
endOfDay(date: Moment): MomentNavigate to the beginning or end of a day.
Parameters:
date - Source Moment objectReturns: New Moment object at start (00:00:00.000) or end (23:59:59.999) of the day
Usage:
const now = utils.parseISO("2023-10-30T14:30:45.123Z");
const dayStart = utils.startOfDay(now); // 2023-10-30T00:00:00.000Z
const dayEnd = utils.endOfDay(now); // 2023-10-30T23:59:59.999ZstartOfWeek(date: Moment): Moment
endOfWeek(date: Moment): MomentNavigate to the beginning or end of a week. Respects locale-specific week start day.
Parameters:
date - Source Moment objectReturns: New Moment object at start or end of the week
Usage:
const wednesday = utils.parseISO("2023-10-25T14:30:00.000Z"); // Wednesday
const weekStart = utils.startOfWeek(wednesday); // Sunday or Monday depending on locale
const weekEnd = utils.endOfWeek(wednesday); // Saturday or Sunday depending on localestartOfMonth(date: Moment): Moment
endOfMonth(date: Moment): MomentNavigate to the beginning or end of a month.
Parameters:
date - Source Moment objectReturns: New Moment object at start (1st day) or end (last day) of the month
Usage:
const midMonth = utils.parseISO("2023-10-15T14:30:00.000Z");
const monthStart = utils.startOfMonth(midMonth); // 2023-10-01T00:00:00.000Z
const monthEnd = utils.endOfMonth(midMonth); // 2023-10-31T23:59:59.999ZstartOfYear(date: Moment): Moment
endOfYear(date: Moment): MomentNavigate to the beginning or end of a year.
Parameters:
date - Source Moment objectReturns: New Moment object at start (Jan 1) or end (Dec 31) of the year
Usage:
const midYear = utils.parseISO("2023-06-15T14:30:00.000Z");
const yearStart = utils.startOfYear(midYear); // 2023-01-01T00:00:00.000Z
const yearEnd = utils.endOfYear(midYear); // 2023-12-31T23:59:59.999ZgetNextMonth(date: Moment): Moment
getPreviousMonth(date: Moment): MomentNavigate to the same day in the next or previous month.
Parameters:
date - Source Moment objectReturns: New Moment object in the next/previous month
Usage:
const october15 = utils.parseISO("2023-10-15T14:30:00.000Z");
const november15 = utils.getNextMonth(october15); // 2023-11-15T14:30:00.000Z
const september15 = utils.getPreviousMonth(october15); // 2023-09-15T14:30:00.000Z
// Handles month boundary edge cases
const oct31 = utils.parseISO("2023-10-31T14:30:00.000Z");
const nov30 = utils.getNextMonth(oct31); // 2023-11-30T14:30:00.000Z (Nov has only 30 days)// Complex date manipulation workflow
const startDate = utils.parseISO("2023-10-15T09:30:00.000Z");
// Move to next month, then to start of that month, then add 2 weeks
const result = utils.addWeeks(
utils.startOfMonth(
utils.getNextMonth(startDate)
),
2
);
// Result: 2023-11-15T00:00:00.000Z
// Or using intermediate variables for clarity
const nextMonth = utils.getNextMonth(startDate);
const startOfNextMonth = utils.startOfMonth(nextMonth);
const twoWeeksLater = utils.addWeeks(startOfNextMonth, 2);