days difference between 2 dates

  • Thread starter Thread starter Claudia Fong
  • Start date Start date
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
 
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
 
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.
 
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
 
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
 
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
 
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.
 
Back
Top