Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have two image buttons,when the first button is clicked the time should start and when the second button is pressed the time should stop and calculate the duration of time and diaplay all the info like start,end time and duration(it should display once the second button is pressed)

Thank you in advance.
Posted
Comments
Peter Leow 27-Mar-15 2:48am    
What have you tried?
Raghu S Gowda 27-Mar-15 2:56am    
Thank you.

i noted the time of btn1 click event n also noted the time of btn2 click event.
in btn2 click event im trying to subtract the both noted times bt im nt able to access the btn1 time variable in btn2 click event method

This code persists the start time between requests

XML
<asp:Button ID="btnStart" runat="server" Text="Start" OnClick="btnStart_Click" />
<asp:Button ID="btnStop" runat="server" Text="Stop" OnClick="btnStop_Click" />

<p>
    Started: <asp:Literal ID="LiteralStart" runat="server" /><br />
    Stopped: <asp:Literal ID="LiteralStop" runat="server" /><br />
    Duration: <asp:Literal ID="LiteralDuration" runat="server" /><br />
</p>


protected void btnStart_Click(object sender, EventArgs e)
{
    DateTime dt = DateTime.Now;
    LiteralStart.Text = dt.ToString();

    ViewState["startTime"] = dt;
}

protected void btnStop_Click(object sender, EventArgs e)
{
    DateTime dt = DateTime.Now;
    LiteralStop.Text = dt.ToString();

    DateTime startTime = (DateTime)ViewState["startTime"];

    TimeSpan duration = dt - startTime;

    LiteralDuration.Text = duration.TotalSeconds.ToString();
}
 
Share this answer
 
Comments
Raghu S Gowda 27-Mar-15 5:39am    
Thank you so much its working perfectly.
i need only time but here its showing both date and time can you do the needful.
and also can i store those values to db because you have used literal tags.
F-ES Sitecore 27-Mar-15 5:59am    
To get the time for the duration

LiteralDuration.Text = string.Format("{0:00}:{1:00}:{2:00}", duration.Hours, duration.Minutes, duration.Seconds);

to get just the date from a DateTime use ToString("<date format="">"), so it would be

dt.ToString("M/dd/yyyy")

You'd have to get the date from the DateTime, you can't get it from the duration timespan.
Raghu S Gowda 27-Mar-15 6:02am    
thank you Sitecore.
for starttime and endtime i need only time to display but here date is also showing.
F-ES Sitecore 27-Mar-15 6:08am    
dt.ToString("HH:mm:ss")

You can see the date formatting options here

https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
Raghu S Gowda 27-Mar-15 6:15am    
Thank you so much.
every thing sorted,hope you help me whenever i'm stuck with something.
So set up two button Click event handlers, and a Form level private DateTime variable.
On one timer, set the variable to the current date and time, and on the other, subtract it from the current date and time. You then have a TimeSpan you can display:
C#
private DateTime startTime = DateTime.Now;
void butStart_Click(Object sender, EventArgs e)
   {
   startTime = DateTime.Now;
   }
void butShow_Click(Object sender, EventArgs e)
   {
   TimeSpan diff = DateTime.Now - startTime;
   labDifference.Text = diff.ToString();
   }
 
Share this answer
 
Comments
Raghu S Gowda 27-Mar-15 3:11am    
Thank you Griff,
Bt i should dispaly the start time and end time in the btn2 click event.
i'm getting the duration like 00:00:00.0640033 for 11sec gap between the two click events.
also its showing the date with the start time ,i need onl start time to display
OriginalGriff 27-Mar-15 3:51am    
Show the exact code you are using - I'm betting it's nothing like what I gave you...:laugh:
Raghu S Gowda 27-Mar-15 4:50am    
public partial class entry : System.Web.UI.Page
{
string con = System.Configuration.ConfigurationManager.ConnectionStrings["CRMConnectionString2"].ConnectionString;
private DateTime startTime = DateTime.Now;
protected void answer_Click(object sender, ImageClickEventArgs e)
{
//string starttime = System.DateTime.Now.ToLongTimeString();
startTime = DateTime.Now;

}
protected void hangup_Click(object sender, ImageClickEventArgs e)
{
Lbl_date.Text = System.DateTime.Now.ToShortDateString();
//string endtime = System.DateTime.Now.ToLongTimeString();
// string duration =endtime - starttime;
//Lbl_dur.Text = duration.ToString();

TimeSpan diff = DateTime.Now - startTime;
Lbl_dur.Text = diff.ToString();
//Lbl_st.Text = starTime;
//Lbl_et.Text = endtime;
Pan_Comm.Visible = true;

}


the output is like this:
Duration:: 00:00:00.6678976
Date:: 3/27/2015


also how to add the starttime and endtime in the hangup event
F-ES Sitecore 27-Mar-15 5:10am    
Probably because startTime isn't persisted between the two button clicks, you get a new startTime each time you press a button, so the elapsed time you're seeing when you click "stop" is the time between the page request for the stop button click starting and the stop click event firing which is always going to be incredibly small.
Raghu S Gowda 27-Mar-15 5:28am    
yes....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900