Display TimeSpan correctly in SSRS

狂风中的少年 提交于 2019-12-12 04:05:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!