JsonLogic.Net icon indicating copy to clipboard operation
JsonLogic.Net copied to clipboard

Compare Two Datetimes

Open draxtor opened this issue 4 years ago • 2 comments

Hello, does this implementation supports comparing two datetimes. I've tried this:

  1. If I check dates it always returns True
var now = DateTime.UtcNow.ToString(); 
string jsonText = "{\"<=\": [{\"var\": \"InvoiceDate.ToString()\"}, \"" + now + "\" ]}";
var now = DateTime.UtcNow.AddDays(-5).ToString("yyyy-MM-dd"); 
string jsonText = "{\"<=\": [{\"var\": \"InvoiceDate.ToString(`yyyy-MM-dd`)\"}, \"" + now + "\" ]}";
  1. Using ISO dates I'm getting exception about invalid input
now = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"); //
jsonText = "{\"<=\": [{\"var\": \"InvoiceDate.ToString(`yyyy-MM-ddTHH:mm:ssZ`)\"}, \"" + now + "\" ]}";

  1. What is also interesting, when I use string property it fails again. Why is this "System.FormatException: 'Input string was not in a correct format.'"
{{
  "<=": [
    {
      "var": "InvoiceDateString"
    },
    "2021-07-20T16:44:12Z"
  ]
}}

Is there any way I can compare two dates?

And if the answer is yes is there a way to use functions? I would like to check against current DateTime in runtime.

Thanks

draxtor avatar Jul 20 '21 14:07 draxtor

@draxtor, I think there is an error in the jsonText you provided. It does a string comparison between "InvoiceDate.ToString()" and whatever now is. The problem is that "var" operation does not do what you are expecting in this context. "var" only references values from the data context. It does not run C# expressions. So, when it processes "InvoiceDate.ToString()", it will first try to get "InvoiceDate" from the data context, which will resolve to null. Then it will try to get ToString() out of that, which will also result in null.

I am assuming that you want to compare 2 date variables. So, first, you will need to add them in your data context. So, something like:

var data = new
{
    Date1 = DateTime.Now,
    Date2 = DateTime.Now.AddDays(4),
};

In your json logic, then, you can compare these two:

var jsonText = @"{\"<\": [{\"var\": \"Date1\"}, {\"var\": \"Date2\"}] }";

yavuztor avatar Aug 08 '21 11:08 yavuztor

Ah sorry my ticket was incomplete (most probably copy mistake). My use case is to test DateTIme property against literal not to compare 2 date properties (but I'll try that one as well) Besides ToString and using string Property I also tried

  • Using date property - does not work
var now = DateTime.UtcNow.AddDays(-5).ToString("yyyy-MM-dd");
string jsonText = "{\"<=\": [{\"var\": \"InvoiceDate\"}, \"" + now + "\" ]}";
  • Using string Property of a date property - does not work
public string InvoiceDateString { get {return InvoiceDate.ToString("yyyy-MM-dd");} }
var now = DateTime.UtcNow.AddDays(-5).ToString("yyyy-MM-dd");
string jsonText = "{\"<=\": [{\"var\": \"InvoiceDateString\"}, \"" + now + "\" ]}";
  • Using long Property of a date property - works
public long InvoiceDateLong { get {return InvoiceDate.ToTimeStamp(); }
var now = DateTime.UtcNow.AddDays(-5).ToTimeStamp(); 
string jsonText = "{\"<=\": [{\"var\": \"InvoiceDateLong\"}, " + now + "]}";

I forked your repo and saw that issue was in file EvaluateOperators.cs, GenericArgsSatisfy method. Since my date literal is parsed as datetime by Newtonsoft.Json and is not text then Double conversion is tried and of course it fails. I made some changes there and it was working, but it was just a test change not candidate for PR.

draxtor avatar Aug 11 '21 08:08 draxtor