Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

days difference between 2 dates

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Claudia Fong - 12 Jun 2007 16:03 GMT
Hi,

In VB we have DateDiff to calculate the days difference between 2 dates,
I was wondering if we

have something like that in C#?

I want to calculate for example the days difference betwenn 19/06/2007
and 12/06/2007.. it

should return 7

Cheers!

   Claudi
eusebiu - 12 Jun 2007 16:17 GMT
use TimeSpan structure : TimeSpan ts = date1 - date2;
Alex Meleta - 12 Jun 2007 16:18 GMT
Hi Claudi,

E.g. for DateTime dt1 = new DateTime(2007, 6, 19) and DateTime dt2 = new
DateTime(2007, 6, 12) you can use dt1.Subtract(dt2).Days to get days diff
(7).

Alex
http://devkids.blogspot.com

> Hi,
>
[quoted text clipped - 13 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
cjard - 12 Jun 2007 17:29 GMT
>you can use dt1.Subtract(dt2).Days to get days diff

Just a note on .Days  vs .TotalDays

If (dt1 - dt2) is 7 days, 12 hours, TotalDays will give 7.5, .Days
will give 7
Christof Nordiek - 12 Jun 2007 16:18 GMT
> Hi,
>
[quoted text clipped - 7 lines]
>
> should return 7

Try:

(date2 - date1).TotalDays;

HTH

Christof
Nicholas Paldino [.NET/C# MVP] - 12 Jun 2007 16:20 GMT
Claudia,

   You should just be able to subtract the two datetime instances and get a
TimeSpan instance.  From that, you can use the Days property to find the
number of days between the two dates.

   If the DateDiff has logic that the standard operations on DateTime does
not provide, then you can add a reference to Microsoft.VisualBasic.dll and
then use the DateDiff function like you would in VB.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi,
>
[quoted text clipped - 13 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
Cor Ligthert [MVP] - 12 Jun 2007 18:02 GMT
Claudia,

As you can see is it not clear wat for you is for you the difference between
2 dates, is that in hours 48 hours or just that from 1 january until 3
january is 2 dates.

(I would avoid the timespan by the day because it has only a limited amount
of days you can get returned).

Cor

> Hi,
>
[quoted text clipped - 13 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
Christof Nordiek - 12 Jun 2007 18:07 GMT
> (I would avoid the timespan by the day because it has only a limited
> amount of days you can get returned).

The maximu value of TimeSpan is 10675199.02:48:05.4775808
I suppose this is enough for most cases ;-)

Christof
Aneesh Pulukkul[MCSD.Net] - 12 Jun 2007 18:30 GMT
On Jun 12, 10:02 pm, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
> Claudia,
>
[quoted text clipped - 26 lines]
>
> - Show quoted text -

dt1.Subtract(dt2)).Days should be enough for the req it seems.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.