On C# DateTime, how many days between d1 and d2?

I have two instances of DateTime, d1 & d2.
How to calculate how many days between d1 and d2?

Thanks a lot.

I went to Microsoft website but couldn't find any
help there.

Jennifer
jfz2004Asked:
Who is Participating?
 
eternal_21Connect With a Mentor Commented:
Here is a simple method:

  int CountDays(DateTime dateFrom, DateTime dateTo, bool including)
  {
    return ((System.TimeSpan)(dateTo - dateFrom)).Days + (including ? 1 : 0);
  }

  including = true if you want to include the last day (dateTo) in your count.
0
 
Timbo87Commented:
TimeSpan t = d2 - d1;

TimeSpan has many methods to retrieve the difference in various formats. Check the .NET SDK for more information.
0
 
jfz2004Author Commented:
thanks.
0
 
jfz2004Author Commented:
Just looked into TimeSpan.
Seems like TimeSpan doesn't have a constructor that
needs two DateTime.

How to resolve that?
0
 
eternal_21Commented:
> "Seems like TimeSpan doesn't have a constructor that needs two DateTime."

What are you trying to do, or have you got the problem figured out?
0
All Courses

From novice to tech pro — start learning today.