This it can be in VB 2008
\\\
Dim EndDate = DateTime.Now
Dim StartDate = Now.AddDays(-3)
Dim days = EndDate - StartDate
MessageBox.Show (days.Days.ToSt ring)
///
This it can be in C# 2008
\\\\
var EndDate = DateTime.Now;
var StartDate = DateTime.Now.Ad dDays(-3);
var days = EndDate - StartDate;
MessageBox.Show (days.Days.ToSt ring());
///
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" <andyoye@nospam .comschreef in bericht
news:O4Oql4KJJH A.4248@TK2MSFTN GP05.phx.gbl...
>I have two date fields(startDat e, endDate) in a form and I want to
>calculate number of days between startDate & endDate.
>
Thanks
>
startDate field value is 10-01-09
endDate field value is 10-05-09
How can I get 4 .....(endDate-startDate)
"Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
news:umQG1LLJJH A.1160@TK2MSFTN GP05.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.ToSt ring)
///
>
This it can be in C# 2008
\\\\
var EndDate = DateTime.Now;
var StartDate = DateTime.Now.Ad dDays(-3);
var days = EndDate - StartDate;
MessageBox.Show (days.Days.ToSt ring());
///
>
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" <andyoye@nospam .comschreef in bericht
news:O4Oql4KJJH A.4248@TK2MSFTN GP05.phx.gbl...
>>I have two date fields(startDat e, endDate) in a form and I want to
>>calculate number of days between startDate & endDate.
>>
>Thanks
>>
10: XPathNavigator dateDiffTextBox Value =
this.CreateNavi gator().SelectS ingleNode("/my:myFields/my:dateDiff",
this.NamespaceM anager);
11: dateDiffTextBox Value.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.get@enough.s pamwrote in message
news:eicRybLJJH A.1968@TK2MSFTN GP06.phx.gbl...
"andyoye" <andyoye@nospam .comwrote in message
news:ekPoOSLJJH A.5696@TK2MSFTN GP06.phx.gbl...
>
>startDate field value is 10-01-09
>endDate field value is 10-05-09
>>
>How can I get 4 .....(endDate-startDate)
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.get@enough.s pamschreef in bericht
news:eicRybLJJH A.1968@TK2MSFTN GP06.phx.gbl...
"andyoye" <andyoye@nospam .comwrote in message
news:ekPoOSLJJH A.5696@TK2MSFTN GP06.phx.gbl...
>
>startDate field value is 10-01-09
>endDate field value is 10-05-09
>>
>How can I get 4 .....(endDate-startDate)
"Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
news:C4021018-66CD-4130-A1E9-4D660D0C14CD@mi crosoft.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!"
"Jeff Johnson" <i.get@enough.s pamwrote in message
news:egvb$SNJJH A.4280@TK2MSFTN GP04.phx.gbl...
"Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
news:C4021018-66CD-4130-A1E9-4D660D0C14CD@mi crosoft.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!"
>
"andyoye" <andyoye@nospam .comwrote in message
news:OZpUlqVJJH A.1156@TK2MSFTN GP05.phx.gbl...
>I already said: You ALL Rocks, thanks.
>
How about getting minutes difference? I mod the days code
>
TimeSpan timeDifference = endTime.Subtrac t(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?
"Jeff Johnson" <i.get@enough.s pamwrote in message
news:OxWMYCYJJH A.4672@TK2MSFTN GP05.phx.gbl...
"andyoye" <andyoye@nospam .comwrote in message
news:OZpUlqVJJH A.1156@TK2MSFTN GP05.phx.gbl...
>
>>I already said: You ALL Rocks, thanks.
>>
>How about getting minutes difference? I mod the days code
>>
>TimeSpan timeDifference = endTime.Subtrac t(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?
>
"andyoye" <andyoye@nospam .comwrote in message
news:eDYM3k6JJH A.1556@TK2MSFTN GP03.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_HOU R = 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.).
Comment