473,569 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Subtrac t(startTime); //calculate timespan
lblTimeSpan.Tex t = ts.Minutes.ToSt ring(); //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 7889
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.Subtrac t(startTime); //calculate timespan
lblTimeSpan.Tex t = ts.Minutes.ToSt ring(); //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
2311
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, minutes....this are...
2
19083
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 defined for the Date datatype. On the other hand, Dim Elapsed as DateTime = Now + Now does not give a compile time error but it thinks you're...
10
38612
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
355
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 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...
2
2936
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 = TimeSpan.Zero End Function Error : = is not corret
13
2128
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 will fall somewhere between a Start & End time Further more, there will be Break & Lunch times between Start & End. Example... Start 08:00 Break...
4
3282
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 following format 15:36:12 ==Hours, minutes, seconds but sometimes there is a string like 55:26:32 ==which is correct cause the production machine was...
4
6228
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 HOUR .... if i try to Call A.Select( " HOUR '10:30:00' " ) i receive an error ( cannot compare TimeSpan with String ...) if i try to call...
4
2772
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 this much code so far. DateTime startDate = new DateTime("2008-02-04"); DateTime curDate = DateTime.Now(); TimeSpan ts =...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.