Home > Software design >  How to get the current date in DataTable.Compute
How to get the current date in DataTable.Compute

Time:09-14

I am trying to compute the string expression by DataTable.Compute.

A simple string like below is working:

        static void Compare()
        {
            var logicalExpression = "'2022-09-14' <= '2029-12-31'";
            DataTable dt = new DataTable();
            var result = (bool)dt.Compute(logicalExpression, "");
        }

But, the date 2022-09-14 is dynamic, it should be the date for now.

I have tried to replace '2022-09-14' with Now(),GETDATE(). None of them work.

Is there any function to get current date time in Compute?

CodePudding user response:

You can do this like this:

var logicalExpression = "'"   DateTime.Now.ToShortDateString()   "' <= '2029-12-31'";

you can also specify in what format you want the date, see this post for an example.

CodePudding user response:

var logicalExpression = $"#{DateTime.Now:MM/dd/yyyy}# <= #12/31/2029#";

This is the proper way to represent dates in this context. See here for more information.

  • Related