473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

days difference between 2 dates

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

*** Sent via Developersdex http://www.developersdex.com ***
Jun 12 '07 #1
8 5419
use TimeSpan structure : TimeSpan ts = date1 - date2;

Jun 12 '07 #2
"Claudia Fong" <cd********@yahoo.co.ukschrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP03.phx.gbl...
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
Jun 12 '07 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Claudia Fong" <cd********@yahoo.co.ukwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Jun 12 '07 #4
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,

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

*** Sent via Developersdex http://www.developersdex.com ***

Jun 12 '07 #5
>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

Jun 12 '07 #6
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" <cd********@yahoo.co.ukschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Jun 12 '07 #7
"Cor Ligthert [MVP]" <no************@planet.nlschrieb im Newsbeitrag
news:Od*************@TK2MSFTNGP02.phx.gbl...
>
(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
Jun 12 '07 #8
On Jun 12, 10:02 pm, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
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" <cdolphi...@yahoo.co.ukschreef in berichtnews:%2****************@TK2MSFTNGP03.phx.gb l...
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
*** Sent via Developersdexhttp://www.developersdex.com***- Hide quoted text -

- Show quoted text -
dt1.Subtract(dt2)).Days should be enough for the req it seems.

Jun 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
2
by: JAPIO | last post by:
I have two variables which look like: $begin = "23-08-05" $end = "26-08-05" Now i want also to know the dates between them. 24-08-05 25-08-05 How can i do this? Any help would be...
9
by: Mike Fellows | last post by:
i have 2 dates one is todays date and the other is a future date i want to take todays date from ther future date and tell me how many days difference this is thanks in advance Mike...
2
by: tasmontique | last post by:
I am working on an access 2002 flight schedule database. I am new to access but have some basic understanding of sql and vb6 code. I have learned a lot from this website. Thanks much Hopefully...
3
by: Jim | last post by:
Guys I'm rusty. Haven't messed with Access in a while. I'm trying to determine how many workdays (or weekdays) between two dates. I don't think their are any "canned" functions within access -...
3
by: John | last post by:
Hi How can I calculate days from the two dates entered in date time picker fields on winform by a user? Thanks Regards
9
by: sha2484 | last post by:
I need help to count days between 2 dates,where i want to count the days between current date and registeration date.Here is the code that i have write but it only compare the days , it do not...
2
by: Chellie | last post by:
I am using Access 2007. I have two date fields (date receievd and date complete). I need to be able to run a query and find out who is not completing their work within two days of receipt. I tried...
2
Jerry Maiapu
by: Jerry Maiapu | last post by:
I am trying to punch into a table a bunch of dates that fall between two given dates. When compiled,no error but when on run does not do anything..(i.e nothing in the table) Can someone figure...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.