Solved

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

Posted on 2004-03-31
5
2,642 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

624 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