Reputation: 1
Consider that i have list dtArray1
that consist of DateTime
items I mean 10.10.2010
, 10.10.2015
and so on.
Could u say me how find subract of this element ? How i can get diffeerence between 2 years?
for (Int32 index = 1; index < dtArray1.Count;index++)
{
if (dtArray1[index] >= dtArray1[index - 1])
{
dtArray1[index].Subtract(dtArray1[index - 1]);
Console.WriteLine(dtArray1[index]);
Console.ReadLine();
}
}
Upvotes: 0
Views: 362
Reputation: 29243
If your dates are of the DateTime
type, you can just substract one from the other, like so:
var span = dtArray1[index] - dtArray1[index - 1];
Console.WriteLine(span.Days); // prints "1836"
The result will be of type TimeSpan. TimeSpan does not have a Years
property, so you'll have to calculate the number of years yourself, keeping in mind leap years and whatnot. See How do I calculate someone's age in C#?
Upvotes: 2