Skip to main content

How to compare two dates in a Power Script

Comparing date Fields can be tricky with plain JavaScript. A reliable way to compare two date Fields in Informer is the built-in Moment.js library, available in scripts as moment.

For example, this Power Script compares two date Fields and adds a new column that says whether they are the same or different:

// Replace "date1" and "date2" with your field aliases
if (moment($record.date1).isSame($record.date2)) {
$record.newColumn = "Same";
} else {
$record.newColumn = "Different";
}

The result includes a new column that says whether the two date Fields are the same or different:

Order Date and Shipped Date columns beside a Same Or Different column reading Same where the two dates match and Different where they don't

isSame() compares down to the millisecond. If your date Fields carry times and you only care about the calendar day, compare by day instead: moment($record.date1).isSame($record.date2, 'day').

Moment.js is the recommended way to work with dates in Informer scripts. For more on Moment.js: