Use VB DateDiff function in C#

A

andyoye

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

Thanks
 
A

Alberto Poblacion

andyoye said:
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;
 
C

Cor Ligthert[MVP]

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
 
A

andyoye

startDate field value is 10-01-09
endDate field value is 10-05-09

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

andyoye

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
 
C

Cor Ligthert[MVP]

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
 
J

Jeff Johnson

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!"
 
A

andyoye

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.
 
J

Jeff Johnson

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?
 
A

andyoye

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
 
J

Jeff Johnson

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.).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top