Time Calculation
-
Hai! How to Calculate Total Elapsed time from given starting value and ending value when the the given values are greater than 24(ie Meter Readings)(for eg:Starting time=1015:35,Ending time=1058:15) Plz Send any Ideas, Thank You
-
Hai! How to Calculate Total Elapsed time from given starting value and ending value when the the given values are greater than 24(ie Meter Readings)(for eg:Starting time=1015:35,Ending time=1058:15) Plz Send any Ideas, Thank You
If you convert those strings to DateTime structures (possibly using DateTime.ParseExact, I don't know the exact form of the strings you gave) then you can simply subtract the starting time from the ending time to get the elapsed time as an instance of the TimeSpan structure. That lets you access such properties as TotalHours, TotalMinutes, TotalSeconds and TotalMilliseconds
-
what do u mean by 1015:35 is it H:M if it's TimeSpan end = new TimeSpan(1015, 35, 0); TimeSpan start = new TimeSpan(1058, 15, 0); TimeSpan sub= end.Subtract(start); double TotalSeconds = sub.TotalSeconds;
DateTime date1 = System.Convert.ToDateTime(txtstarttime.Text); DateTime date2 = System.Convert.ToDateTime(txtendtime.Text); TimeSpan ts = new TimeSpan(); ts = date2.Subtract(date1); DateTime dt = DateTime.MinValue.Add(ts); txttottime.Text=ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds ;
I have used this but it Produce error when the given values are greater than 24
-
DateTime date1 = System.Convert.ToDateTime(txtstarttime.Text); DateTime date2 = System.Convert.ToDateTime(txtendtime.Text); TimeSpan ts = new TimeSpan(); ts = date2.Subtract(date1); DateTime dt = DateTime.MinValue.Add(ts); txttottime.Text=ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds ;
I have used this but it Produce error when the given values are greater than 24
-
What format are txtstarttime.Text and txtendtime.Text in? Are they hours:minutes:seconds like your output? If so, then you can replace the
System.Convert.ToDateTime
calls withSystem.DateTime.Parse("input goes here", "hh:mm:ss", null)
-
DateTime date1 = System.Convert.ToDateTime(txtstarttime.Text); DateTime date2 = System.Convert.ToDateTime(txtendtime.Text); TimeSpan ts = new TimeSpan(); ts = date2.Subtract(date1); DateTime dt = DateTime.MinValue.Add(ts); txttottime.Text=ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds ;
I have used this but it Produce error when the given values are greater than 24
-
Hi, Hmm, you need to stop for a moment and think about what you are trying to do. If the measured time span is 24 hours or more then information about the number of days that have passed is required to perform the calculation. If the time span is guaranteed to be less than 24 hours the following pseudocode is valid
// Calculate time span
duration = endtime - starttime;
// if the value is negative then assume that
// midnight has passed
// and therefore end day = start day + 1
if duration < 0 then duration = duration + 24 hours;Note how this would fail as soon as the actual time span is 24 hours or more. Alan. [EDIT corrected error in code fragment]
modified on Sunday, May 31, 2009 9:29 AM