or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

calendar.mdcomponents.mddate-creation.mdformatting.mdindex.mdlocalization.mdmanipulation.mdvalidation-comparison.md
tile.json

components.mddocs/

Date Component Access

This capability provides getter and setter methods for accessing and modifying individual date components (year, month, day, hour, etc.). All setter methods return new Moment instances, preserving immutability.

Time Components

Hours

getHours(date: Moment): number
setHours(date: Moment, count: number): Moment

Get or set the hour component of a date (0-23).

Parameters:

  • date - Source Moment object
  • count - Hour value to set (0-23)

Returns: Number for getter, new Moment object for setter

Usage:

const date = utils.parseISO("2023-10-30T14:30:45.123Z");

// Get current hour
const currentHour = utils.getHours(date); // 14

// Set specific hour
const morningTime = utils.setHours(date, 9); // 2023-10-30T09:30:45.123Z
const eveningTime = utils.setHours(date, 18); // 2023-10-30T18:30:45.123Z
const midnight = utils.setHours(date, 0); // 2023-10-30T00:30:45.123Z

Minutes

getMinutes(date: Moment): number
setMinutes(date: Moment, count: number): Moment

Get or set the minute component of a date (0-59).

Parameters:

  • date - Source Moment object
  • count - Minute value to set (0-59)

Returns: Number for getter, new Moment object for setter

Usage:

const date = utils.parseISO("2023-10-30T14:30:45.123Z");

// Get current minutes
const currentMinutes = utils.getMinutes(date); // 30

// Set specific minutes
const quarterPast = utils.setMinutes(date, 15); // 2023-10-30T14:15:45.123Z
const halfPast = utils.setMinutes(date, 30); // 2023-10-30T14:30:45.123Z
const topOfHour = utils.setMinutes(date, 0); // 2023-10-30T14:00:45.123Z

Seconds

getSeconds(date: Moment): number
setSeconds(date: Moment, count: number): Moment

Get or set the second component of a date (0-59).

Parameters:

  • date - Source Moment object
  • count - Second value to set (0-59)

Returns: Number for getter, new Moment object for setter

Usage:

const date = utils.parseISO("2023-10-30T14:30:45.123Z");

// Get current seconds
const currentSeconds = utils.getSeconds(date); // 45

// Set specific seconds
const roundMinute = utils.setSeconds(date, 0); // 2023-10-30T14:30:00.123Z
const halfSecond = utils.setSeconds(date, 30); // 2023-10-30T14:30:30.123Z

Date Components

Year

getYear(date: Moment): number
setYear(date: Moment, year: number): Moment

Get or set the year component of a date.

Parameters:

  • date - Source Moment object
  • year - Full year value to set (e.g., 2023, 2024)

Returns: Number for getter, new Moment object for setter

Usage:

const date = utils.parseISO("2023-10-30T14:30:45.123Z");

// Get current year
const currentYear = utils.getYear(date); // 2023

// Set specific year
const nextYear = utils.setYear(date, 2024); // 2024-10-30T14:30:45.123Z
const pastYear = utils.setYear(date, 2020); // 2020-10-30T14:30:45.123Z

// Handles leap year edge cases
const feb29_2020 = utils.parseISO("2020-02-29T12:00:00.000Z"); // Leap year
const feb28_2021 = utils.setYear(feb29_2020, 2021); // 2021-02-28T12:00:00.000Z (not a leap year)

Month

getMonth(date: Moment): number
setMonth(date: Moment, count: number): Moment

Get or set the month component of a date. Note: Months are 0-indexed (0 = January, 11 = December).

Parameters:

  • date - Source Moment object
  • count - Month value to set (0-11, where 0 = January)

Returns: Number for getter (0-11), new Moment object for setter

Usage:

const date = utils.parseISO("2023-10-30T14:30:45.123Z"); // October

// Get current month (0-indexed)
const currentMonth = utils.getMonth(date); // 9 (October)

// Set specific month
const january = utils.setMonth(date, 0); // 2023-01-30T14:30:45.123Z
const december = utils.setMonth(date, 11); // 2023-12-30T14:30:45.123Z

// Handles month boundary edge cases
const jan31 = utils.parseISO("2023-01-31T12:00:00.000Z");
const feb28 = utils.setMonth(jan31, 1); // 2023-02-28T12:00:00.000Z (Feb doesn't have 31 days)

Day of Month

getDate(date: Moment): number
setDate(date: Moment, day: number): Moment

Get or set the day of the month (1-31).

Parameters:

  • date - Source Moment object
  • day - Day of month to set (1-31)

Returns: Number for getter, new Moment object for setter

Usage:

const date = utils.parseISO("2023-10-30T14:30:45.123Z");

// Get current day of month
const currentDate = utils.getDate(date); // 30

// Set specific day
const firstOfMonth = utils.setDate(date, 1); // 2023-10-01T14:30:45.123Z
const fifteenth = utils.setDate(date, 15); // 2023-10-15T14:30:45.123Z
const endOfMonth = utils.setDate(date, 31); // 2023-10-31T14:30:45.123Z

// Handles invalid days gracefully
const february = utils.parseISO("2023-02-15T12:00:00.000Z");
const feb28 = utils.setDate(february, 31); // 2023-02-28T12:00:00.000Z (Feb doesn't have 31 days)

Additional Date Components

Week Number

getWeek(date: Moment): number

Gets the week number of the year (1-53) according to ISO 8601 standard.

Parameters:

  • date - Source Moment object

Returns: Week number (1-53)

Usage:

const date = utils.parseISO("2023-10-30T14:30:45.123Z");
const weekNumber = utils.getWeek(date); // Week number in 2023

const newYear = utils.parseISO("2023-01-01T12:00:00.000Z");
const week1 = utils.getWeek(newYear); // 52 or 1 (depending on day of week)

Days in Month

getDaysInMonth(date: Moment): number

Gets the number of days in the month of the given date.

Parameters:

  • date - Source Moment object

Returns: Number of days in the month (28-31)

Usage:

const october = utils.parseISO("2023-10-15T12:00:00.000Z");
const octoberDays = utils.getDaysInMonth(october); // 31

const february = utils.parseISO("2023-02-15T12:00:00.000Z");
const februaryDays = utils.getDaysInMonth(february); // 28

const leapFebruary = utils.parseISO("2020-02-15T12:00:00.000Z");
const leapFebDays = utils.getDaysInMonth(leapFebruary); // 29

Combined Component Operations

You can chain component setters to build specific dates:

// Build a specific date: January 1, 2024 at 9:30:00 AM
const baseDate = utils.date(); // Current date
const specificDate = utils.setSeconds(
  utils.setMinutes(
    utils.setHours(
      utils.setDate(
        utils.setMonth(
          utils.setYear(baseDate, 2024),
          0  // January
        ),
        1  // 1st day
      ),
      9  // 9 AM
    ),
    30  // 30 minutes
  ),
  0  // 0 seconds
);

// Or more readable with intermediate variables
const year2024 = utils.setYear(baseDate, 2024);
const january = utils.setMonth(year2024, 0);
const firstDay = utils.setDate(january, 1);
const nineAM = utils.setHours(firstDay, 9);
const halfPast = utils.setMinutes(nineAM, 30);
const exactTime = utils.setSeconds(halfPast, 0);

Component Range Validation

Remember that setters handle invalid values gracefully by clamping to valid ranges:

const date = utils.parseISO("2023-01-15T12:00:00.000Z");

// Invalid month (>11) gets clamped
const invalidMonth = utils.setMonth(date, 15); // Becomes month 11 (December)

// Invalid day gets clamped to last day of month
const february = utils.setMonth(date, 1); // February
const invalidDay = utils.setDate(february, 31); // Becomes Feb 28 (or 29 in leap year)

// Invalid hour gets wrapped
const invalidHour = utils.setHours(date, 25); // Becomes hour 1 of next day

// Invalid minute/second gets wrapped
const invalidMinute = utils.setMinutes(date, 75); // Becomes 15 minutes of next hour