Questions

how to calculate the time duration?

Tags:
+
0 Votes
Locked

how to calculate the time duration?

heaven23
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#
  • +
    0 Votes
    Tony Hopkinson

    are the same....

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

    +
    0 Votes
    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.

    +
    0 Votes
    Mehul Bhai

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

    +
    0 Votes
    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.

    Happens to us all mate.

    +
    0 Votes

    :)

    heaven23

    thank you very much sir

  • +
    0 Votes
    Tony Hopkinson

    are the same....

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

    +
    0 Votes
    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.

    +
    0 Votes
    Mehul Bhai

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

    +
    0 Votes
    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.

    Happens to us all mate.

    +
    0 Votes

    :)

    heaven23

    thank you very much sir