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

validation-comparison.mddocs/

Date Validation and Comparison

This capability provides methods for validating dates and comparing date relationships including equality, ordering, and range checking.

Validation Methods

isValid(value: any): boolean

Checks if a value can be converted to a valid Moment date.

Parameters:

  • value - Any value to test for date validity

Returns: true if value creates a valid date, false otherwise

Usage:

const valid = utils.isValid("2023-10-30"); // true
const invalid = utils.isValid("invalid-date"); // false
const validDate = utils.isValid(new Date()); // true
const validMoment = utils.isValid(utils.date()); // true
isNull(date: Moment): boolean

Checks if a date value is null.

Parameters:

  • date - Moment object or null to check

Returns: true if date is null, false otherwise

Usage:

const nullDate = null;
const realDate = utils.date();
console.log(utils.isNull(nullDate)); // true
console.log(utils.isNull(realDate)); // false

Equality Comparison

isEqual(value: any, comparing: any): boolean

Checks if two date values represent the same moment in time. Handles null values gracefully.

Parameters:

  • value - First date value to compare
  • comparing - Second date value to compare

Returns: true if dates represent the same moment, false otherwise

Usage:

const date1 = utils.parseISO("2023-10-30T14:30:00.000Z");
const date2 = utils.parseISO("2023-10-30T14:30:00.000Z");
const date3 = utils.parseISO("2023-10-30T15:30:00.000Z");

console.log(utils.isEqual(date1, date2)); // true
console.log(utils.isEqual(date1, date3)); // false
console.log(utils.isEqual(null, null)); // true
console.log(utils.isEqual(null, date1)); // false

Temporal Ordering

isAfter(date: Moment, value: Moment): boolean

Checks if the first date is after the second date.

Parameters:

  • date - Date to compare
  • value - Reference date

Returns: true if date is after value, false otherwise

isBefore(date: Moment, value: Moment): boolean

Checks if the first date is before the second date.

Parameters:

  • date - Date to compare
  • value - Reference date

Returns: true if date is before value, false otherwise

Usage:

const earlier = utils.parseISO("2023-10-30T14:30:00.000Z");
const later = utils.parseISO("2023-10-30T15:30:00.000Z");

console.log(utils.isAfter(later, earlier)); // true
console.log(utils.isBefore(earlier, later)); // true
console.log(utils.isAfter(earlier, later)); // false

Day-Level Comparisons

isAfterDay(date: Moment, value: Moment): boolean
isBeforeDay(date: Moment, value: Moment): boolean
isSameDay(date: Moment, comparing: Moment): boolean

Compare dates with day-level precision, ignoring time components.

Parameters:

  • date - Date to compare
  • value/comparing - Reference date

Returns: Boolean result of the comparison

Usage:

const morning = utils.parseISO("2023-10-30T09:00:00.000Z");
const evening = utils.parseISO("2023-10-30T21:00:00.000Z");
const nextDay = utils.parseISO("2023-10-31T09:00:00.000Z");

console.log(utils.isSameDay(morning, evening)); // true (same day)
console.log(utils.isAfterDay(nextDay, morning)); // true
console.log(utils.isBeforeDay(morning, nextDay)); // true

Month-Level Comparisons

isAfterMonth(date: Moment, value: Moment): boolean
isBeforeMonth(date: Moment, value: Moment): boolean
isSameMonth(date: Moment, comparing: Moment): boolean

Compare dates with month-level precision, ignoring day and time components.

Parameters:

  • date - Date to compare
  • value/comparing - Reference date

Returns: Boolean result of the comparison

Usage:

const october1 = utils.parseISO("2023-10-01T12:00:00.000Z");
const october31 = utils.parseISO("2023-10-31T12:00:00.000Z");
const november1 = utils.parseISO("2023-11-01T12:00:00.000Z");

console.log(utils.isSameMonth(october1, october31)); // true
console.log(utils.isAfterMonth(november1, october1)); // true
console.log(utils.isBeforeMonth(october1, november1)); // true

Year-Level Comparisons

isAfterYear(date: Moment, value: Moment): boolean
isBeforeYear(date: Moment, value: Moment): boolean
isSameYear(date: Moment, comparing: Moment): boolean

Compare dates with year-level precision, ignoring month, day, and time components.

Parameters:

  • date - Date to compare
  • value/comparing - Reference date

Returns: Boolean result of the comparison

Usage:

const jan2023 = utils.parseISO("2023-01-01T12:00:00.000Z");
const dec2023 = utils.parseISO("2023-12-31T12:00:00.000Z");
const jan2024 = utils.parseISO("2024-01-01T12:00:00.000Z");

console.log(utils.isSameYear(jan2023, dec2023)); // true
console.log(utils.isAfterYear(jan2024, jan2023)); // true
console.log(utils.isBeforeYear(jan2023, jan2024)); // true

Hour-Level Comparisons

isSameHour(date: Moment, comparing: Moment): boolean

Checks if two dates are in the same hour.

Parameters:

  • date - First date to compare
  • comparing - Second date to compare

Returns: true if dates are in the same hour, false otherwise

Usage:

const time1 = utils.parseISO("2023-10-30T14:15:00.000Z");
const time2 = utils.parseISO("2023-10-30T14:45:00.000Z");
const time3 = utils.parseISO("2023-10-30T15:15:00.000Z");

console.log(utils.isSameHour(time1, time2)); // true (both in 14:xx hour)
console.log(utils.isSameHour(time1, time3)); // false (different hours)

Range Validation

isWithinRange(date: Moment, range: [Moment, Moment]): boolean

Checks if a date falls within a specified range (inclusive of boundaries).

Parameters:

  • date - Date to check
  • range - Tuple containing [start, end] dates

Returns: true if date is within range (inclusive), false otherwise

Usage:

const checkDate = utils.parseISO("2023-10-15T12:00:00.000Z");
const startDate = utils.parseISO("2023-10-01T00:00:00.000Z");
const endDate = utils.parseISO("2023-10-31T23:59:59.000Z");

console.log(utils.isWithinRange(checkDate, [startDate, endDate])); // true

const outsideDate = utils.parseISO("2023-11-01T12:00:00.000Z");
console.log(utils.isWithinRange(outsideDate, [startDate, endDate])); // false

Date Difference Calculation

getDiff(date: Moment, comparing: Moment | string, unit?: Unit): number

Calculates the difference between two dates in the specified unit.

Parameters:

  • date - First date
  • comparing - Second date (Moment object or string)
  • unit - Unit for difference calculation (defaults to milliseconds)

Returns: Numeric difference in the specified unit

Usage:

const date1 = utils.parseISO("2023-10-30T14:30:00.000Z");
const date2 = utils.parseISO("2023-10-30T15:30:00.000Z");

console.log(utils.getDiff(date2, date1, "hours")); // 1
console.log(utils.getDiff(date2, date1, "minutes")); // 60
console.log(utils.getDiff(date1, date2, "hours")); // -1 (date1 is before date2)

// With invalid comparing date, returns 0
console.log(utils.getDiff(date1, "invalid-date", "hours")); // 0