Date Difference

Hi all,

I have generated two sets of long integers corresponding to dates:
Year1, Month1, Day1
Year2, Month2, Day2

I would like to use these to generate a new variable containing the days and one containing the months elapsed between these two dates.

I was looking into C# documentation, but I can’t seem to make the DateTime calculations required.

Can anyone help me out?

Best,

Giacomo

Giacomo,

date difference is of class TimeSpan documented here:

To construct the DateTime from three components, use the DateTime constructor with Year, Month, and Day as documented here:

Note that you will need to cast your longs to ints:
new DateTime((int)Y,(int)M.Value,(int)D)

For days difference use the TotalDays property of a TimeSpan object.
There is no TotalMonths property for the reasons of ambiguity explained here:

Best, Sergiy

tdays=((new DateTime((int)year_int, (int)month_int, (int)day_int))
- (new DateTime((int)year_final_nc, (int)month_final_nc, (int)day_final_nc))).TotalDays

Dear Sergiy,

thanks so much for your answer!

I think I’m getting close: this is what I have in my variable now

(Func(() =>
{
DateTime dateint = DateTime((int)year_int, (int)month_int, (int)day_int);
DateTime datebirth = DateTime((int)year_final_nc, (int)month_final_nc, (int)day_final_nc);
TimeSpan interval = dateint - datebirth;
double result = interval.TotalDays;
return result;
})).Invoke()

This still gives an error in SS. When I try to compile this in C#, I get the following:
A namespace cannot directly contain members such as fields or methods

Can you help? I really have no experience in C#, so this might be really trivial.

Thanks,

Giacomo