如何将SSRS中的时间格式化为HH:MM AM/PM格式?

sac*_*ith 7 datetime reporting-services

我的任务是增加以下代码以显示没有秒的时间,并使用AM/PM:

=IIF(Fields!New_Date.Value <> "NO CHANGE", FormatDateTime(IIF(Fields!New_Date.Value = "NO CHANGE","1/1/12",Fields!New_Date.Value),DateFormat.ShortDate), "") &
IIF(Fields!New_Time.Value <> "NO CHANGE",FormatDateTime(IIF(Fields!New_Time.Value = "NO CHANGE","00:00",Fields!New_Time.Value),DateFormat.ShortTime), "")
Run Code Online (Sandbox Code Playgroud)

在意识到FormatDateTime不足以满足我的目的时,我发现以下内容不起作用(仅查看与时间字段相关的snippit),:

Format(IIF(Fields!New_Time.Value = "NO CHANGE","00:00",Fields!New_Time.Value),"HH:mm tt")  
Run Code Online (Sandbox Code Playgroud)

或这个

Format(IIF(Fields!New_Time.Value = "NO CHANGE","00:00",Fields!New_Time.Value),"HH:MM tt")
Run Code Online (Sandbox Code Playgroud)

我从这里得到格式代码.

我究竟做错了什么?

sac*_*ith 8

所以我发现,因为我本质上是在尝试格式化文本框,所以SSRS从未真正"知道"它是一个时间字段.虽然我不是100%确定我的推理是正确的,但它确实解释了为什么以下有效:

Format(CDate(IIF(Fields!New_Time.Value = "NO CHANGE","00:00",Fields!New_Time.Value)),"hh:mm tt")
Run Code Online (Sandbox Code Playgroud)


Gil*_*etz 7

如果您的数据位于简单文本框(或表格单元格)中,则您可以使用更简单的解决方案.

右键单击文本框>属性:

使用以下内容: 在此输入图像描述


hur*_*lee 7

The other answers are correct, but the key difference to know with the format is:

 h:mm tt --> 12-hour clock (e.g. 5:30 PM)
hh:mm tt --> 12-hour clock with a leading zero, if necessary (e.g. 05:30 PM)
 H:mm    --> 24-hour clock (e.g. 1:30 for 1:30 AM)
HH:mm    --> 24-hour clock with a leading zero, if necessary (e.g. 01:30 for 1:30 AM)
Run Code Online (Sandbox Code Playgroud)


小智 5

从文本框属性 -> 数字 -> 自定义类别:

尝试输入以下内容:

日-月-年 时:分 tt