This capability provides methods for validating dates and comparing date relationships including equality, ordering, and range checking.
isValid(value: any): booleanChecks if a value can be converted to a valid Moment date.
Parameters:
value - Any value to test for date validityReturns: 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()); // trueisNull(date: Moment): booleanChecks if a date value is null.
Parameters:
date - Moment object or null to checkReturns: 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)); // falseisEqual(value: any, comparing: any): booleanChecks if two date values represent the same moment in time. Handles null values gracefully.
Parameters:
value - First date value to comparecomparing - Second date value to compareReturns: 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)); // falseisAfter(date: Moment, value: Moment): booleanChecks if the first date is after the second date.
Parameters:
date - Date to comparevalue - Reference dateReturns: true if date is after value, false otherwise
isBefore(date: Moment, value: Moment): booleanChecks if the first date is before the second date.
Parameters:
date - Date to comparevalue - Reference dateReturns: 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)); // falseisAfterDay(date: Moment, value: Moment): boolean
isBeforeDay(date: Moment, value: Moment): boolean
isSameDay(date: Moment, comparing: Moment): booleanCompare dates with day-level precision, ignoring time components.
Parameters:
date - Date to comparevalue/comparing - Reference dateReturns: 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)); // trueisAfterMonth(date: Moment, value: Moment): boolean
isBeforeMonth(date: Moment, value: Moment): boolean
isSameMonth(date: Moment, comparing: Moment): booleanCompare dates with month-level precision, ignoring day and time components.
Parameters:
date - Date to comparevalue/comparing - Reference dateReturns: 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)); // trueisAfterYear(date: Moment, value: Moment): boolean
isBeforeYear(date: Moment, value: Moment): boolean
isSameYear(date: Moment, comparing: Moment): booleanCompare dates with year-level precision, ignoring month, day, and time components.
Parameters:
date - Date to comparevalue/comparing - Reference dateReturns: 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)); // trueisSameHour(date: Moment, comparing: Moment): booleanChecks if two dates are in the same hour.
Parameters:
date - First date to comparecomparing - Second date to compareReturns: 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)isWithinRange(date: Moment, range: [Moment, Moment]): booleanChecks if a date falls within a specified range (inclusive of boundaries).
Parameters:
date - Date to checkrange - Tuple containing [start, end] datesReturns: 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])); // falsegetDiff(date: Moment, comparing: Moment | string, unit?: Unit): numberCalculates the difference between two dates in the specified unit.
Parameters:
date - First datecomparing - 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