days difference between 2 dates

C

Claudia Fong

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
 
C

Christof Nordiek

Claudia Fong said:
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

Try:

(date2 - date1).TotalDays;

HTH

Christof
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
C

cjard

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
 
C

Cor Ligthert [MVP]

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
 
C

Christof Nordiek

Cor Ligthert said:
(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
 
A

Aneesh Pulukkul[MCSD.Net]

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

"Claudia Fong" <[email protected]> schreef in bericht







- Show quoted text -

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top