Question

Locked

how to calculate the time duration?

By 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#

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Because currenttime1 & 2

by Tony Hopkinson In reply to how to calculate the time ...

are the same....

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

Collapse -

No sir....

by heaven23 In reply to Because currenttime1 & 2

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.

Collapse -

Zero

by Mehul Bhai In reply to No sir....

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

Collapse -

No it isn't. you set both

by Tony Hopkinson In reply to No sir....

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.

Collapse -

:)

by heaven23 In reply to No it isn't. you set both

thank you very much sir

Back to Web Development Forum
6 total posts (Page 1 of 1)  

Software Forums