-
Is there a method to enable the json-rules-engine to determine "if current date is lessThan 2025-06-30"? I can pass the current date as a fact, but how can I compare a date similar to how you can compare numbers? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I tried adding the current date as a fact:
And then reference that in my rule as so:
And it seemed to work at first, but doesn't work fully. |
Beta Was this translation helpful? Give feedback.
-
So I tried using it like:
And I'm setting a fact like: If you log that, you get the ms since Unix epoch: And then a rule like:
And it works as expected with: But for most practical purposes, 'equal', 'notEqual', 'lessThanInclusive', 'greaterThanInclusive' are not going to be useful then, as you're essentially never going to be comparing exactly that ms of time. I only want to look at the actual days involved, not the time. And I want the same results whether it's 5am or 10pm local time - not based on UTC. If it's being used to set something based on the date, it needs to be the date where I live. So I've setup a function like this (yeah, vanilla JavaScript sucks with handling dates, and I could shorten this, but like this for clarity):
And then set the fact as this:
And then setup the addOperatorDecorator like this:
(not a one-liner so that I can log values within the function for testing) And the rule can be the same as above. Then my date numeric values are for the start of the day in the local timezone, and look something like: 1738908000000 This lets me use any of these for date logic: Hopefully this helps someone else. @chris-pardy Thanks for your help on this - would it be worthwhile adding this to the documentation for handling dates? |
Beta Was this translation helpful? Give feedback.
So I tried using it like:
engine.addOperatorDecorator('dateValue', (factValue, value, next) => next(factValue, new Date(value).getTime()));
And I'm setting a fact like:
currentDate = new Date().getTime();
If you log that, you get the ms since Unix epoch:
currentDate 1738883049146
And then a rule like:
And it works as expected with:
'lessThan', 'greaterThan'
But for most practical purposes, 'equal', 'notEqual', 'lessThanInclusive', 'greaterThanInclusive' are not going to be useful then, as you're essentially never going to be comparing exact…