Question Locked

how to calculate the time duration?

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

hi.. please check this code of mine:

protected void Button4_Click(object sender, EventArgs e)
{
DateTime dt1;
DateTime dt2;

string currenttime1 = DateTime.Now.ToLongTimeString();
string currenttime2 = DateTime.Now.ToLongTimeString();
TextBox2.Text = currenttime2;

dt1 = Convert.ToDateTime(currenttime1);
dt2 = Convert.ToDateTime(currenttime2);

TimeSpan ts = dt1 - dt2;

int hours = ts.Hours;
int min = ts.Minutes;

string finalMin = Convert.ToString(min);

TextBox3.Text = finalMin;

---- i'm having a web page that calculated time duration when the user press the stop button.. but the output of this code is always zero.. what is my error?
thanks in advance..
--im using asp.net and C#

6 replies

by tony hopkinson

are the same....

A guess would be textbox1 has the start time.
Did you want the span to be negative as well?

by heaven23

ahm.. the currenttime1 is from the time that was get when the START button is clicked. for example, i clicked the start button at time: 10:03 pm... so currenttime1 is: 10:03 pm... i clicked the STOP button at 11:00 pm.. currenttime2 is now 11:00.

by 93961

I am not a programmer, but from what you posted it seems that your final answer is bound to be zero.

by tony hopkinson

string currenttime1 = DateTime.Now.ToLongTimeString();
string currenttime2 = DateTime.Now.ToLongTimeString();

to Now.

Based on what you've said currenttime1 should be textbox1.text converted to a datetime which presumably was set from the start button.

One of those slaps forehead, Homer Simpson Doh moments. :p

Happens to us all mate.

This discussion is closed to new replies.