问题
I have a TimeSpan value of 00:02:02.0
basically 0 hours 2 minutes 2 seconds and 2 milliseconds.
I will always have 0 hours. How do you format ssrs to read 2.02.0? I know you can do =Fields!FinishTime.Value.ToString("mm:ss:fff") however it always produces #error as value.
Thanks!
回答1:
You are pretty close, ToString()
is correct since SSRS doesn't know how to work with TimeSpans, but you'll need to wrap a Format
around it to use the format string and convert it back to a date to use the date formatting patterns:
=Format(CDate(Fields!FinishTime.Value.ToString()), "mm:ss:fff")
EDIT:
If your dataset contains NULLs, you won't be able to use a simple IIF(IsNothing(Fields!FinishTime.Value), Nothing, <format formula>)
because IIF does not short-circuit and you'll see an #error when it tries to use the ToString() function on NULLs. So instead try some custom code (retrieved from an MSDN forum post):
Function FormatTimeSpan(TS as TimeSpan) as String
Dim DT as new DateTime(TS.Ticks)
Return DT.ToString("mm:ss:fff")
End Function
And call it like this:
=IIF(IsNothing(Fields!FinishTime.Value), Nothing, Code.FormatTimeSpan(Fields!FinishTime.Value))
来源:https://stackoverflow.com/questions/25170189/display-timespan-correctly-in-ssrs