Solved

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

Posted on 2004-03-31
5
2,636 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:jfz2004
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:Timbo87
ID: 10726817
TimeSpan t = d2 - d1;

TimeSpan has many methods to retrieve the difference in various formats. Check the .NET SDK for more information.
0
 

Author Comment

by:jfz2004
ID: 10726820
thanks.
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 50 total points
ID: 10726826
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
 

Author Comment

by:jfz2004
ID: 10726877
Just looked into TimeSpan.
Seems like TimeSpan doesn't have a constructor that
needs two DateTime.

How to resolve that?
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 10726966
> "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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now