Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare Two Datetimes #29

Open
draxtor opened this issue Jul 20, 2021 · 2 comments
Open

Compare Two Datetimes #29

draxtor opened this issue Jul 20, 2021 · 2 comments

Comments

@draxtor
Copy link

draxtor commented Jul 20, 2021

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

@yavuztor
Copy link
Owner

yavuztor commented Aug 8, 2021

@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\"}] }";

@draxtor
Copy link
Author

draxtor commented Aug 11, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants