473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Another C# TimeSpan Question

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.

Jul 21 '05 #1
2 7872
it looks like you are capturing the start time and end time in two different events (post backs). Is the start time object still valid when you come back for the click event of the second button ?
Here is a small article about finding difference between two time obejcts : http://dotnetspider.com/Technology/KBPages/552.aspx

"Justin Grieves" wrote:
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.

Jul 21 '05 #2
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.

"ToJo" wrote:
it looks like you are capturing the start time and end time in two different events (post backs). Is the start time object still valid when you come back for the click event of the second button ?
Here is a small article about finding difference between two time obejcts : http://dotnetspider.com/Technology/KBPages/552.aspx

"Justin Grieves" wrote:
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).


Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dirk Reske | last post by:
hey, when I do: TimeSpan ts = TimeSpan.FromTicks(System.Enviroment.TickCount); ts.ToString(); I get something like this: 00:00:00.2381474 but I want to see, how many hours,...
2
by: DWalker | last post by:
In Visual Studio (Visual Basic) .NET 2002, I noticed that this: Dim Elapsed as DateTime = Now - Now gives a "compile time" error (error in the IDE), saying that the '-' operator is not...
10
by: Charles Law | last post by:
If I display a TimeSpan I get something like 00:05:17.6217891 when what I would like to see is 00:05:18 Is there an easy way to get this output? Try as I might I just can't find it.
2
by: Justin Grieves | last post by:
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...
2
by: ucasesoftware | last post by:
i translate a C# funtion to VB.NET and i have this : Shared Function isAllDay(ByVal ap As Appointment) As Boolean Return ap.DateBegin.TimeOfDay = TimeSpan.Zero AndAlso ap.DateEnd.TimeOfDay =...
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
4
by: Peter Proost | last post by:
Hi group, it's been a long time since the last time I've been here but I have a question. I'm working with timespan.parse for calculating a duration, I have to add strings which are in the...
4
by: Massimo | last post by:
Hi to All, i'm using C# in .NET 2.0 and i have a DataTable A with a column of type TimeSpan used to store HOUR info. I'm trying to filter my DataTable A selecting only rows that have the column...
4
by: kellygreer1 | last post by:
I haven't worked with the TimeSpan object before. So bare with me if this seems like a newb question. But if I wanted to know how many days from the current date until 2/4/2008. I have written...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.