C# – Time Elapsed/StopWatch

ajaxcelapsedstopwatchtime

I want to create an ASP.NET WinForms application that tracks time on task. I need to be able to write the form so that I can add the task to the database, open it in a new tab, and be able to start, pause, and stop the task. When I'm finished, I need to calculate the time taken to complete the task. I would like to have a view of the stopwatch running on the page, showing hours:min:sec updating every second via AJAX. I have already looked on the web at TimeSpan, DateTime, StopWatch, etc. and I can't seem to find anything that works for me. I started with a simple form with start and stop buttons. The _click event for the start button assigns my DateTime variable 'startTime = DateTime.Now' and the _click event for the stop button assigns my DateTime variable 'endTime = DateTime.Now'. I then use a TimeSpan 'elapsed' to calculate TimeSpan 'elapsed = (endTime – startTime). When I update a label to show the time elapsed, I'm expecting to get just the seconds that have gone by, but I get the entire DateTime string. Below is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace CS_StopWatch
{
    public partial class Default : System.Web.UI.Page
    {
        //public Stopwatch myStopWatch = new Stopwatch();
        public DateTime startTime;
        public DateTime endTime;
        public TimeSpan ts_timeElapsed;
        public string s_timeElapsed;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void StartButton_Click(object sender, EventArgs e)
        {
            //myStopWatch.Start();
            startTime = DateTime.Now;
        }

        protected void StopButton_Click(object sender, EventArgs e)
        {
            //myStopWatch.Stop();
            //ElapsedLabel.Text = "Time Elapsed: " + myStopWatch.Elapsed;
            endTime = DateTime.Now;
            ts_timeElapsed = (endTime - startTime);
            s_timeElapsed = GetElapsedTimeString();
            ElapsedLabel.Text = "Time Elapsed: " + s_timeElapsed;
        }

        public string GetElapsedTimeString()
        {
            int days = ts_timeElapsed.Days;
            double hours = ts_timeElapsed.Hours;
            double mins = ts_timeElapsed.Minutes;
            double secs = ts_timeElapsed.Seconds;
            string x = "";
            if (days != 0)
            {
                x += days.ToString() + ":";
            }
            if (hours != 0)
            {
                x += hours.ToString() + ":";
            }
            if (mins != 0)
            {
                x += mins.ToString() + ":";
            }
            if (secs != 0)
            {
                x += secs.ToString();
            }

            return x;
        }
    }
}

Best Answer

I'm not sure if this causes your problem, but you should use int instead of double, as the TimeSpan members are int anyway. Comparing double to an exact number can cause problems

        int hours = ts_timeElapsed.Hours;
        int mins = ts_timeElapsed.Minutes;
        int secs = ts_timeElapsed.Seconds;