Add Times?

  • Thread starter Thread starter James Goodman
  • Start date Start date
J

James Goodman

I have modified the ASP.NET TimeTracker Starter Kit to include the ability
for users to log working hours (Start Time, Finish Time, Lunch Duration).
This is then presented to them in a grid & a TimeWorked column is calculated
(in a UDF within the DB).

I want to include a label in the footer of TimeWorked which will show the
TotalTimeWorked in the format HH:mm (e.g. 37:30).

All of these TimeWorked values are returned in the format #1/1/1900
7:30:00AM# which indicates a total of 07:30 for the day. This is formatted
in the grid & displays as desired.

I notice that you can subtract one datetime from another, but you can only
add a timespan a datetime. I therefore created a timespan by subtracting
#1/1/1900 00:00:00# from the TimeWorked value. I then added this timespan to
a Total timespan. Unfortunately, this does not appear to calculate correctly
with example values:

TimeWorked
07:50
11:25
08:00
07:50
05:40

As I loop through these the values in tsTotal are:
tsTotal.Hours:tsTotal.Minutes
7:50
19:15
3:15 (Starts going wrong here?)
11:5
16:45


How can I add these values to get the Sum(TimeWorked)?
 
Hi James,

You don't want to add times. You want to subtract them. Subtracting the
Start Time from the End Time will result in a TimeSpan of the difference
between them Subtracting LunchDuration from that TimeSpan will give you the
net time worked.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
I have a SQL UDF which already gives the TimeWorked for each day, but I want
to add these up & display the total for the data in the grid (will normally
be for the selected week).
 
Ah, well, in that case just add them all together as TimeSpans.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
I thought that, but when I added each timespan to my total timespan it went
kind of haywire.

I am not sure if it is because it is going beyond 24 hours & subsequently
trying to count in days?
 
I must be going mad!

It was incrementing into days (e.g. 37:30 = 1d13h30m), but I am sure when I
looked at this property earlier it was displaying a crazy value. I think I
must have been looking at the TotalDays property.

Oh well, thanks anyway. :)
 
A TimeSpan has multiple components, just like a DateTime does. For example,
a Time Span of 6 hours, 12 minutes and 30 seconds has 6 hours, 12 minutes,
and 30 seconds in it. So if you reference YourTimeSpan.Minutes, the return
value will be 12, not 732 as you might expect. To get the total minutes
(732) out of it, you would refer to YourTimeSpan.TotalMinutes.

Does that help?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
Back
Top