473,659 Members | 2,944 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5433
use TimeSpan structure : TimeSpan ts = date1 - date2;

Jun 12 '07 #2
"Claudia Fong" <cd********@yah oo.co.ukschrieb im Newsbeitrag
news:%2******** ********@TK2MSF TNGP03.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).TotalDay s;

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.Visua lBasic.dll and
then use the DateDiff function like you would in VB.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Claudia Fong" <cd********@yah oo.co.ukwrote in message
news:%2******** ********@TK2MSF TNGP03.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(dt 2).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(dt 2).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********@yah oo.co.ukschreef in bericht
news:%2******** ********@TK2MSF TNGP03.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.nlschri eb im Newsbeitrag
news:Od******** *****@TK2MSFTNG P02.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...@yah oo.co.ukschreef in berichtnews:%2* *************** @TK2MSFTNGP03.p hx.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 Developersdexht tp://www.developersd ex.com***- Hide quoted text -

- Show quoted text -
dt1.Subtract(dt 2)).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
4397
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: starttime = Date.parse("Aug 10,2003, 07:07") sdt = new Date(starttime)
2
1842
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 appreciated.
9
3224
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 Fellows
2
3110
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 you can help me with this one. This database handles a varying number of flights for a varying number of days for a varying number of months. I have a number of tables as follows. I tried to normalize to the best of my ability.
3
2403
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 - or is there? Can someone help with a simple function count(vbweekdays, startdate,enddate), less stat holidays (LOL!)
3
4302
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
11583
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 compare the month and the year, where it will return 0 if the day of the second date is the same as the first date even the month or year are different, plz help me... my code: public static int dayDiff(java.sql.Date date1, java.sql.Date date2)throws...
2
5980
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 using datediff but can't figure it out. Any thoughts? I would appreciate it! Chellie
2
1567
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 out what's missing? Option Compare Database Option Explicit Private Sub cmdinsert_Click()
0
8335
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.