473,403 Members | 2,071 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,403 software developers and data experts.

Use VB DateDiff function in C#

I have two date fields(startDate, endDate) in a form and I want to
calculate number of days between startDate & endDate.

Thanks
Oct 2 '08 #1
12 1784
"andyoye" <an*****@nospam.comwrote in message
news:O4**************@TK2MSFTNGP05.phx.gbl...
>I have two date fields(startDate, endDate) in a form and I want to
calculate number of days between startDate & endDate.
int days = ((TimeSpan)(endDate-startDate)).Days;
Oct 2 '08 #2
Hi Andy,

DateDiff in VB is a little bit from pre history.

This it can be in VB 2008
\\\
Dim EndDate = DateTime.Now
Dim StartDate = Now.AddDays(-3)
Dim days = EndDate - StartDate
MessageBox.Show(days.Days.ToString)
///

This it can be in C# 2008
\\\\
var EndDate = DateTime.Now;
var StartDate = DateTime.Now.AddDays(-3);
var days = EndDate - StartDate;
MessageBox.Show(days.Days.ToString());
///

I real don't see much differences.

The VB code only in this case to show that the question is a little bit
strange.

Cor
"andyoye" <an*****@nospam.comschreef in bericht
news:O4**************@TK2MSFTNGP05.phx.gbl...
>I have two date fields(startDate, endDate) in a form and I want to
calculate number of days between startDate & endDate.

Thanks
Oct 2 '08 #3
startDate field value is 10-01-09
endDate field value is 10-05-09

How can I get 4 .....(endDate-startDate)

"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:um**************@TK2MSFTNGP05.phx.gbl...
Hi Andy,

DateDiff in VB is a little bit from pre history.

This it can be in VB 2008
\\\
Dim EndDate = DateTime.Now
Dim StartDate = Now.AddDays(-3)
Dim days = EndDate - StartDate
MessageBox.Show(days.Days.ToString)
///

This it can be in C# 2008
\\\\
var EndDate = DateTime.Now;
var StartDate = DateTime.Now.AddDays(-3);
var days = EndDate - StartDate;
MessageBox.Show(days.Days.ToString());
///

I real don't see much differences.

The VB code only in this case to show that the question is a little bit
strange.

Cor
"andyoye" <an*****@nospam.comschreef in bericht
news:O4**************@TK2MSFTNGP05.phx.gbl...
>>I have two date fields(startDate, endDate) in a form and I want to
calculate number of days between startDate & endDate.

Thanks

Oct 2 '08 #4
"andyoye" <an*****@nospam.comwrote in message
news:ek**************@TK2MSFTNGP06.phx.gbl...
startDate field value is 10-01-09
endDate field value is 10-05-09

How can I get 4 .....(endDate-startDate)
Alberto gave you the answer.
Oct 2 '08 #5
Below is smaple code from my form

{

1: XPathNavigator docFM = this.CreateNavigator();

2: string date_form = docFM.SelectSingleNode("/my:myFields/my:startDate",
this.NamespaceManager).Value;

3: XPathNavigator docFN = this.CreateNavigator();

4: string date_to = docFN.SelectSingleNode("/my:myFields/my:startTime",
this.NamespaceManager).Value;

5: DateTime startDate = Convert.ToDateTime(date_frorm);

6: DateTime endDate = Convert.ToDateTime(date_to);

7: TimeSpan dateDifference = endDate.Subtract(startDate);

8: int days = dateDifference.Days;

9: string daysString = days.ToString();

10: XPathNavigator dateDiffTextBoxValue =
this.CreateNavigator().SelectSingleNode("/my:myFields/my:dateDiff",
this.NamespaceManager);

11: dateDiffTextBoxValue.SetValue(daysString);

}

I get
"String was not recognized as a valid DateTime." at line 6
"When convertin a string to DateTime, parse the string to take the date
before putting each variable into the DateTime object"

thx

"Jeff Johnson" <i.***@enough.spamwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
"andyoye" <an*****@nospam.comwrote in message
news:ek**************@TK2MSFTNGP06.phx.gbl...
>startDate field value is 10-01-09
endDate field value is 10-05-09

How can I get 4 .....(endDate-startDate)

Alberto gave you the answer.

Oct 2 '08 #6
Jeff,

Because this is in my answering thread.

I am curious, what is beside that I use C# 2008 code and Alberto C# 2003
code in your idea the difference between those two answers. Don't understand
me wrong, nothing wrong with Alberto its code, however you give the
impression there is something wrong with mine?

Cor

"Jeff Johnson" <i.***@enough.spamschreef in bericht
news:ei**************@TK2MSFTNGP06.phx.gbl...
"andyoye" <an*****@nospam.comwrote in message
news:ek**************@TK2MSFTNGP06.phx.gbl...
>startDate field value is 10-01-09
endDate field value is 10-05-09

How can I get 4 .....(endDate-startDate)

Alberto gave you the answer.
Oct 2 '08 #7
I got it guys, I was passing a wrong var

thanks all.. you all rocks!!!
"andyoye" <an*****@nospam.comwrote in message
news:Op**************@TK2MSFTNGP06.phx.gbl...
Below is smaple code from my form

{

1: XPathNavigator docFM = this.CreateNavigator();

2: string date_form = docFM.SelectSingleNode("/my:myFields/my:startDate",
this.NamespaceManager).Value;

3: XPathNavigator docFN = this.CreateNavigator();

4: string date_to = docFN.SelectSingleNode("/my:myFields/my:startTime",
this.NamespaceManager).Value;

5: DateTime startDate = Convert.ToDateTime(date_frorm);

6: DateTime endDate = Convert.ToDateTime(date_to);

7: TimeSpan dateDifference = endDate.Subtract(startDate);

8: int days = dateDifference.Days;

9: string daysString = days.ToString();

10: XPathNavigator dateDiffTextBoxValue =
this.CreateNavigator().SelectSingleNode("/my:myFields/my:dateDiff",
this.NamespaceManager);

11: dateDiffTextBoxValue.SetValue(daysString);

}

I get
"String was not recognized as a valid DateTime." at line 6
"When convertin a string to DateTime, parse the string to take the date
before putting each variable into the DateTime object"

thx

"Jeff Johnson" <i.***@enough.spamwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
>"andyoye" <an*****@nospam.comwrote in message
news:ek**************@TK2MSFTNGP06.phx.gbl...
>>startDate field value is 10-01-09
endDate field value is 10-05-09

How can I get 4 .....(endDate-startDate)

Alberto gave you the answer.


Oct 2 '08 #8
"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:C4**********************************@microsof t.com...
Jeff,

Because this is in my answering thread.

I am curious, what is beside that I use C# 2008 code and Alberto C# 2003
code in your idea the difference between those two answers. Don't
understand me wrong, nothing wrong with Alberto its code, however you give
the impression there is something wrong with mine?
Wait, are you asking me why I responded the way I did? If so, I was simply
telling andyoye that he was asking for an answer yet again when Alberto had
already given him the answer. In other words, "Pay attention: you've already
been told!"
Oct 2 '08 #9
I already said: You ALL Rocks, thanks.

How about getting minutes difference? I mod the days code

TimeSpan timeDifference = endTime.Subtract(startTime);

int hours = timeDifference.Hours;

string hoursString = hours.ToString();

Now I do get hours but not minutes.

"Jeff Johnson" <i.***@enough.spamwrote in message
news:eg**************@TK2MSFTNGP04.phx.gbl...
"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:C4**********************************@microsof t.com...
>Jeff,

Because this is in my answering thread.

I am curious, what is beside that I use C# 2008 code and Alberto C# 2003
code in your idea the difference between those two answers. Don't
understand me wrong, nothing wrong with Alberto its code, however you
give the impression there is something wrong with mine?

Wait, are you asking me why I responded the way I did? If so, I was simply
telling andyoye that he was asking for an answer yet again when Alberto
had already given him the answer. In other words, "Pay attention: you've
already been told!"

Oct 3 '08 #10
"andyoye" <an*****@nospam.comwrote in message
news:OZ**************@TK2MSFTNGP05.phx.gbl...
>I already said: You ALL Rocks, thanks.

How about getting minutes difference? I mod the days code

TimeSpan timeDifference = endTime.Subtract(startTime);

int hours = timeDifference.Hours;

string hoursString = hours.ToString();

Now I do get hours but not minutes.
Okay, what is the ULTIMATE result you're looking for? In other words, given
two DateTime values, what would you like to see as the result of the
difference between them?
Oct 3 '08 #11
how about time difference to the minutes? below date and time values are
entered by users in text fields.

So startDate = 10-01-2008 startEnd= 2:05 PM
endDate = 10-03-2008 endTime = 1:00 PM

result should be 1days 22 hrs 55mins
"Jeff Johnson" <i.***@enough.spamwrote in message
news:Ox**************@TK2MSFTNGP05.phx.gbl...
"andyoye" <an*****@nospam.comwrote in message
news:OZ**************@TK2MSFTNGP05.phx.gbl...
>>I already said: You ALL Rocks, thanks.

How about getting minutes difference? I mod the days code

TimeSpan timeDifference = endTime.Subtract(startTime);

int hours = timeDifference.Hours;

string hoursString = hours.ToString();

Now I do get hours but not minutes.

Okay, what is the ULTIMATE result you're looking for? In other words,
given two DateTime values, what would you like to see as the result of the
difference between them?

Oct 6 '08 #12
"andyoye" <an*****@nospam.comwrote in message
news:eD**************@TK2MSFTNGP03.phx.gbl...
how about time difference to the minutes? below date and time values are
entered by users in text fields.

So startDate = 10-01-2008 startEnd= 2:05 PM
endDate = 10-03-2008 endTime = 1:00 PM

result should be 1days 22 hrs 55mins
[Sorry for the late reply; I forgot I started following this group!]

The best thing to do is to get the difference between the dates in the
smallest possible unit you're looking for (minutes in this case) and then
apply math to extract the larger units. For example, you've got 2815 minutes
there. You make some constants like

private const int MINUTES_PER_DAY = 1440
private const int MINUTES_PER_HOUR = 60 // Yes, this one might be
overkill....

and then you test your value against them. If you have more than the
constant, then you've got at least one of that unit, so you do some integer
division to get the number of units and then you test the remainder against
the next smaller unit.

Instead of constants, you could make a class which holds the number and a
description (1440 / "days", 60 / "hrs", etc.) and then make an array or
collection of objects of that class so you could process them in a loop.
Then you could make different arrays/collections for different units
(seconds, milliseconds, etc.).
Oct 13 '08 #13

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

Similar topics

2
by: News Central | last post by:
To all! I use the DateDiff function using VB6 and get this error 'Wrong number of argument or invalid property assignment' ... have anyone seen this problem? thanks ....
7
by: Drago | last post by:
Hi, I got the next question, I want to know the diference between two dates and thats is really easy, but my problem is get that diference in the following format ex. "the diference is= 0 year,...
6
by: Lofty | last post by:
Hi all. I have to write an app that interacts with mySQL (I really must have done some evil, evil stuff in a previous life to be landed with this!) I need to work out the difference in days...
1
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days...
4
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS...
1
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a...
5
by: mcbill20 | last post by:
Hello all. I have a really basic question that I hope someone has a better answer for. I apologize in advance-- I know this is probably a really basic question but I am used to Oracle rathern than...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
6
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
2
by: muddasirmunir | last post by:
i am using vb 6 , i had place two datepicker in form now i want to calcuate differcen of month in two date for this i used the function datediff i had try it withh many syntax but getting error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
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,...
0
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...

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.