Reputation: 1469
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?
Upvotes: 4
Views: 3826
Reputation: 3751
This is very late to the party but I have built an alternative option to NCalc called Expressive. This is available as a nuget package also.
It was originally built to match NCalcs functionality so migrating should require only a small amount of effort.
You can do a lot more date related functions:
DaysBetween(#15/02/2013#, #16/02/2013#)
Upvotes: 2
Reputation: 3523
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());
Upvotes: 8
Reputation: 113385
No, the NCalc library doesn't allow to do this.
Read this related topic.
But you can do it withoud of NCalc.
Assuming that a
and b
are of type DateTime
, (a - b).TotalDays
will return the number of days.
Upvotes: 3
Reputation: 570
DateTime Date1, Date2;
Date1 = DateTime.Parse("2013-03-27 8:42:00");
Date2 = DateTime.Parse("2013-03-27 8:42:26");
TimeSpan TimeSpan1 = Date2 - Date1;
double DayDifference = TimeSpan1.TotalDays;
Upvotes: -2