Connecting Tech Pros Worldwide Help | Site Map

Get Time Difference

RN1
Guest
 
Posts: n/a
#1: Oct 4 '08
A Form has 2 TextBoxes where in users enter date & time. Example (in
mm/dd/yyyy format):

09/20/2008 17:54 (1st TextBox)

09/29/2008 6:13 (2nd TextBox)

How do I find out how much time (in hours & minutes) has elapsed
between the 2 datetime values? I need the exact time difference.

Thanks.
Mark Rae [MVP]
Guest
 
Posts: n/a
#2: Oct 4 '08

re: Get Time Difference


"RN1" <rn5a@rediffmail.comwrote in message
news:becf4594-2270-446f-b045-97e592ed09a3@a2g2000prm.googlegroups.com...
Quote:
How do I find out how much time (in hours & minutes) has elapsed
between the 2 datetime values? I need the exact time difference.
http://www.google.co.uk/search?sourc...23%22+TimeSpan


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#3: Oct 4 '08

re: Get Time Difference


RN1 wrote:
Quote:
A Form has 2 TextBoxes where in users enter date & time. Example (in
mm/dd/yyyy format):
>
09/20/2008 17:54 (1st TextBox)
>
09/29/2008 6:13 (2nd TextBox)
>
How do I find out how much time (in hours & minutes) has elapsed
between the 2 datetime values? I need the exact time difference.
>
Thanks.
Use the DateTime.Parse or DateTime.ParseExact method to parse the
strings into DateTime values, then you just subtract one from the other
to get a TimeSpan value. With the TimeSpan value you can use the Hours
and Minutes properties to get the hours and minutes.

--
Göran Andersson
_____
http://www.guffa.com
Cowboy \(Gregory A. Beamer\)
Guest
 
Posts: n/a
#4: Oct 4 '08

re: Get Time Difference


At its simplest, you can do this:

DateTime time1 = DateTime.Parse(TextBox1.Text);
DateTime time2 = DateTime.Parse(TextBox2.Text);

TimeSpan ts = time2 - time1;

int hours = ts.Hours + (ts.Days * 24);
int minutes = ts.Minutes;

This makes a lot of assumptions, however. Assumptions:

1. Textboxes have actual time values
2. Time2 is larger than Time1

If you can verify the assumptions will always be correct, you can roll with
this. If not, you need a safety net. It looks more like this:

DateTime time1, time2;
TimeSpan ts;

bool time1Okay = DateTime.TryParse(TextBox1.Text, out time1);
bool time2Okay = DateTime.TryParse(TextBox2.Text, out time2);

if ((time1Okay) && (time2Okay))
{
int compare = DateTime.Compare(time1, time2);

if (compare < 0)
{
//time 1 is smaller than time 2
ts = time2 - time1;
}
else
{
//time 1 is bigger than time 2
ts = time1 - time2;
}

//Calculate values
int hours = ts.Hours + (ts.Days * 24);
int minutes = ts.Minutes;

//Display Differences here
}
else
{
//Alert user time value(s) is/are bad
}

If you also want seconds, you can do this:

//Calculate values
int hours = ts.Hours + (ts.Days * 24);
int minutes = ts.Minutes;
int seconds = ts.Seconds;

But perhaps you like to go about this the hard way?

double totalSeconds = ts.TotalSeconds;
double workingMinutes = (totalSeconds / 60);
int seconds = (int)(workingMinutes - (int)workingMinutes);
int hours = (int)(workingMinutes / 60);
int minutes = (int)(workingMinutes - (hours *60));

You can also go from the other direction, but you will end up with rounding
errors:

double totalHours = ts.TotalHours;
int hours = (int)totalHours;
double workingMinutes = (totalHours - hours) * 60;
int minutes = (int)workingMinutes;
int seconds = (int)((workingMinutes - minutes) * 60);

Have fun!

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"RN1" <rn5a@rediffmail.comwrote in message
news:becf4594-2270-446f-b045-97e592ed09a3@a2g2000prm.googlegroups.com...
Quote:
>A Form has 2 TextBoxes where in users enter date & time. Example (in
mm/dd/yyyy format):
>
09/20/2008 17:54 (1st TextBox)
>
09/29/2008 6:13 (2nd TextBox)
>
How do I find out how much time (in hours & minutes) has elapsed
between the 2 datetime values? I need the exact time difference.
>
Thanks.
Closed Thread