如何使用日期时间获取毫秒?

时间:2012-06-04 01:56:58

标签: ssrs-2008 reporting-services

我有一个客户希望报告的执行时间显示在他们的报告的基础上。为了适应它们,我在报告(在报告属性下)创建了一个名为'GroupExecutionTime'的变量,其中包含以下表达式:

=System.DateTime.Now

然后在报告的页脚中我有以下内容:

="Execution Time: " +
IIf(Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).TotalSeconds < 1, "0 seconds", 
(
IIf(Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Hours > 0, Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Hours & " hour(s), ", "") +
IIf(Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Minutes > 0, Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Minutes & " minute(s), ", "") +
IIf(Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Seconds > 0, Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Seconds & " second(s)", ""))
)

不幸的是,客户回来说他们希望我显示报告在不到一秒的时间内执行的毫秒数。这是一个低优先级的要求,但好奇心和希望满足要求让我想知道如何做到这一点?不幸的是,System.DateTime.Now似乎没有在秒之后存储任何内容。

修正了以下内容(感谢Anthony Sottile的回答)

="Execution Time: " +
IIf(Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).TotalSeconds < 1, "0." & Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Milliseconds & " seconds.", 
(
IIf(Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Hours > 0, Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Hours & " hour(s), ", "") +
IIf(Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Minutes > 0, Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Minutes & " minute(s), ", "") +
IIf(Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Seconds > 0, Variables!GroupExecutionTime.Value.Subtract(Globals!ExecutionTime).Seconds & " second(s)", ""))
)

1 个答案:

答案 0 :(得分:5)

TimeSpan方法生成的.Subtract()对象应具有可以打印的.Milliseconds属性。

http://msdn.microsoft.com/en-US/library/system.timespan.milliseconds%28v=vs.90%29

我不确定为什么DateTime.Now不会记录ms?如果没有,那么这将没有多大帮助。遗憾的是,我似乎无法启动我的SSRS实例来自己测试...