Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a time span of seconds in hh:mm:ss format in Reporting Services

In MS Reporting Services 2008, I have a field that is a duration stored as seconds. Is there a slick way to get it into hh:mm:ss format in a group section of the report?

like image 808
seanicus Avatar asked Mar 16 '11 21:03

seanicus


People also ask

What is dd hh mm ss?

Days, Hours, Minutes And Seconds (Dd HH:MM:SS) to Second (s), Common Units.


2 Answers

If you need to work with times longer than 24 hours (Chris Latta's solution will wraparound in these cases), then there are a couple of solutions.

Simple Formula

If you want to just use a formula on the field such the following from this thread, (which also links back to this question)!

=int(sum(Fields!Sec_Online.Value)/3600) & ":" & int((sum(Fields!Sec_Online.Value) Mod 3600)/60) & ":" & (sum(Fields!Sec_Online.Value) Mod 3600) Mod 60

If you need to pad your value to 2 characters you can wrap a RIGHT("0" & {X}, 2) around each sub-section, where {x} indicates one of the individual calculations in the above formula.

Code Behind

Another approach, also suggested in this thread, is to use TimeSpan.FromSeconds(doc), and there is an implementation of that on this blog, using custom code behind in the report.

I ended up using the custom code approach (as I had lots of fields sharing this), and combining it with something more like the first method as I didn't want days to start appearing I just wanted hours to count up bigger than 23.

I added some custom code to the report as follows which pads all values to at least 2 characters, and allows hours to hours count up > 23.

Public Function ConvertSecondsToHourMinSec(ByVal intTotalSeconds) As String
    Dim hours As String =INT(intTotalSeconds/3600)
    If Len(hours) < 2 Then
        hours = RIGHT(("0" & hours), 2)
    End If
    Dim mins As String = RIGHT("0" & INT((intTotalSeconds MOD 3600)/60), 2)
    Dim secs AS String = RIGHT("0" & ((intTotalSeconds MOD 3600) MOD 60), 2)

    ConvertSecondsToHourMinSec = hours & ":" & mins & ":" & secs

End Function

and then called this from each cell in questions as follows:

=code.ConvertSecondsToHourMinSec(Fields!MyField.Value)

I hope this helps someone else!

like image 52
xan Avatar answered Sep 20 '22 14:09

xan


If you just want to display it, convert in an expression for the Value of the textbox:

=Format(DateAdd("s", Fields!MySecondsField.Value, "00:00:00"), "HH:mm:ss")

If you want to do calculations on it, convert the seconds to a DateTime in your dataset. Using SQL:

SELECT DATEADD(ss, MySecondsField, '1900-01-01') AS SecondsAsDateTime
FROM TimeTable

In Linq this would be something like:

var qry = from Q in t.TimeList
    select new
    {
        SecondsAsDateTime = DateTime.Today.AddSeconds(Q.MySecondsField) 
    };

Then you can just format it as a normal DateTime.

like image 45
Chris Latta Avatar answered Sep 18 '22 14:09

Chris Latta