Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NCalc date operations

Tags:

c#

ncalc

I am using NCalc in a project. Is there a way to do date operations like

#16/02/2013# - #15/02/2013# = 1

I can't seem to be able to produce a result.

Expression.Evaluate();

Results is null for the above expression. I can compare two dates, but is there a way to do operations on them using NCalc?

like image 704
Daniel Avatar asked Dec 21 '22 07:12

Daniel


1 Answers

You can do this in ncalc quite easily if you are happy to create a custom function.

Expression e = new Expression("DayDiff(#16/02/2013#, #15/02/2013#)");
e.EvaluateFunction += delegate(string name, FunctionArgs args)
{
    if (name == "DayDiff")
    {
        var date1 = args.Parameters[0].Evaluate();
        var date2 = args.Parameters[1].Evaluate();
        var timespan = date2 - date1;
        return timespan.TotalDays; // double (you can convert to int if you wish a whole number!)
    }
}
Console.Write(e.Evaluate());
like image 158
Chris Walsh Avatar answered Jan 09 '23 18:01

Chris Walsh