Skip to main content

How to compare dates

These Calculated Field expressions compare date Fields with Moment.js and return true or false. Replace firstDateAlias, secondDateAlias, and thirdDateAlias with the aliases of your date Fields.

Check if two dates are the same​

Uses Moment.js isSame().

// Date One: August 6, 2022
// Date Two: August 12, 2022
// Result: false

var dateOne = moment($record['firstDateAlias']);
var dateTwo = moment($record['secondDateAlias']);

dateOne.isSame(dateTwo);

isSame() compares down to the millisecond, so two dates on the same day with different times are not the same. To compare only the calendar day, pass a unit as the second argument: dateOne.isSame(dateTwo, 'day').

Check if one date is on or before another date​

Uses Moment.js isSameOrBefore().

// Date One: August 6, 2022
// Date Two: August 12, 2022
// Result: true

var dateOne = moment($record['firstDateAlias']);
var dateTwo = moment($record['secondDateAlias']);

dateOne.isSameOrBefore(dateTwo);

Check if one date is after another date​

Uses Moment.js isAfter().

// Date One: August 6, 2022
// Date Two: August 12, 2022
// Result: false

var dateOne = moment($record['firstDateAlias']);
var dateTwo = moment($record['secondDateAlias']);

dateOne.isAfter(dateTwo);

Check if one date is between two other dates​

Uses Moment.js isBetween().

// Date One: August 12, 2022
// Date Two: July 24, 2022
// Date Three: August 27, 2022
// Result: true

var dateOne = moment($record['firstDateAlias']);
var dateTwo = moment($record['secondDateAlias']);
var dateThree = moment($record['thirdDateAlias']);

dateOne.isBetween(dateTwo, dateThree);

isBetween() leaves out the two boundary dates, so a Date One of July 24, 2022 returns false here. To include the boundaries, pass '[]' as the fourth argument: dateOne.isBetween(dateTwo, dateThree, undefined, '[]').

To compare dates inside a Power Script instead, where the result becomes a new column, see How to compare two dates in a Power Script.