Another C# TimeSpan Question

G

Guest

Hello all,

I've been trying to determine the difference in minutes between two DateTime objects (startTime and endTime) in C# using the TimeSpan object (ts). I am populating the two DateTime objects from the click of two buttons (start and stop) that I have placed on an ASP.NET webform. I have attempted to follow the many examples that are available on the web yet I'm finding that instead of returning the difference in minutes, my code is only returning the minute value of my second DateTime object (endTime).

//Object Definitions

DateTime startTime;
DateTime endTime;
TimeSpan ts;


//Clicking a buton on my webform called "start" processes:

startTime = DateTime.Now; //opening time stamp


//Clicking button called "end" processes:

endTime = DateTime.Now; //closing time stamp
ts = endTime.Subtract(startTime); //calculate timespan
lblTimeSpan.Text = ts.Minutes.ToString(); //write minutes to label


The results are the same if I replace the Subtract method with:

ts = endTime - startTime; //calculate timespan

If I try to return the property of TotalMinutes instead of Minutes, I receive a value that is typed as a double and cannot seem to format into anything meaningful.

Any insight into what I may be doing incorrectly would be greatly appreciated.

Thank you for your time,

Justin.
 
G

Guest

Thanks ToJo, this was an oversight on my part.

When I clicked on my start button it loaded my start DateTime object correctly but clicking the stop button fired another postback which killed my first timestamp and created only the end timestamp. I've adopted a work around that stores my start timestamp as a session variable on the start click and performs the necessary subtraction and subsequent clean up of the session object when I click on the end button.

Just one of those days!

Justin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top