当且仅当时间部分=00:00:00时,如何在显示中抑制.NET DateTime的时间部分?

当且仅当时间部分=00:00:00时,如何在显示中抑制.NET DateTime的时间部分?,.net,asp.net,datetime,eval,datetime-format,.net,Asp.net,Datetime,Eval,Datetime Format,在ASP.NET页面中,我有以下内容: <asp:Label ID="MyDateTimeLabel" runat="server" Text='<%# Eval("MyDateTime") %>' /> 当且仅当MyDateTime的时间部分为00:00:00时。除此之外: ... Eval("MyDateTime", "{0:g}") ... // Display date and time in hh:mm format 这可能吗?我该怎么做 提前感

在ASP.NET页面中,我有以下内容:

<asp:Label ID="MyDateTimeLabel" runat="server" 
     Text='<%# Eval("MyDateTime") %>' />
当且仅当MyDateTime的时间部分为00:00:00时。除此之外:

... Eval("MyDateTime", "{0:g}") ... // Display date and time in hh:mm format
这可能吗?我该怎么做


提前感谢您的提示

我不确定你是否在找这个,但我觉得值得一试。 希望它能起作用

<%# String.Format(Eval("MyDateTime"),"{0:d}") %>

<%# String.Format(Eval("MyDateTime"),"{0:g}") %>

没有测试,但我突然想到:

加价

<asp:Label ID="MyDateTimeLabel" runat="server" 
     Text='<%# FormatMyDateTime((DateTime)Eval("MyDateTime")) %>' />

我会把这个放在我的代码后面:

protected string FormatMyDateTime(DateTime date)
{
      // Do your if else for formatting here.
}
// This could use a better name!
protected string FormatDateHideMidnight(DateTime dateTime) {
    if (dateTime.TimeOfDay == TimeSpan.Zero) {
        return dateTime.ToString("d");
    } else {
        return dateTime.ToString("g");
    }
}
并将.aspx更改为:

<asp:Label ID="MyDateTimeLabel" runat="server" 
     Text='<%# FormatDateHideMidnight((DateTime)Eval("MyDateTime")) %>' />

用于
DateTime
并将此逻辑放在那里(可能带有其他参数以提供不同的格式等)。

您可以在aspx文件中替换以下代码,或者创建一个方法并调用该方法以返回值

<%
   DateTime dtTime = DateTime.Now;

    if (dtTime.TimeOfDay == TimeSpan.Zero)
        Response.Write(String.Format("{0:d}", dtTime));
    else
        Response.Write(String.Format("{0:g}", dtTime));
%>

您没有提到您使用的.net语言。使用VB.NET,可以使用以下内联表达式:

... Text='<%# Eval("MyDateTime", If(Eval("MyDateTime").TimeOfDay = TimeSpan.Zero, "{0:d}", "{0:g}")) %>'
。。。文本=“”

我没有用C#进行测试,但我想用三元
?:
操作符替换
If(…)
,并在访问
TimeOfDay
之前将
Eval
的结果转换为
DateTime
,这样做应该很有效。

只显示日期部分

<asp:Label id="lblExamDate" runat="server" Text='<%#Convert.ToDateTime(Eval("theExamDate.Date")).ToShortDateString()%>'></asp:Label>

并且只显示时间部分

<asp:Label ID="lblStartTime" runat="server" Text='<%#Convert.ToDateTime(Eval("ExamStartTime")).ToShortTimeString()%>' />


谢谢,它工作得很好,除了标记“…as DateTime”需要替换为“…as DateTime?”(代码隐藏中的DateTime参数也必须是DateTime?),因为“as”操作符需要引用类型或可空类型(调试器告诉我)。如果
MyDateTime
DateTime
,则,正确的方法是使用“常规”强制转换(
(DateTime)Eval(“MyDateTime”)
)而不是
as
as
运算符不能与
DateTime
一起使用,因为它是不可为空的值类型。@João-感谢您指出这一点。。。就像我说的,“我没有测试,但我的脑袋是这么想的:”;)@海因茨:在我的项目中,MyDateTime可以为null,也可以为非null。因此,在不可为空的情况下,我将使用您的建议“(DateTime)Eval(“MyDateTime”)”,但对于可为空的datetimes,“Eval”(“MyDateTime”)作为DateTime?“。代码隐藏中的方法作为“FormatMyDateTime(DateTime?DateTime)”只需要一次。它也适用于可为空的日期时间和不可为空的日期时间。(当然,在格式化之前,我必须在方法中捕获null大小写)。这就是要走的路吗?谢谢你提供了“扩展方法”的链接。我不知道这是什么(除了我听过这个词),我来看看。实际上是的,我想在几个地方和几页中使用这个日期时间格式。我已经将代码放入工具名称空间和静态类中。到目前为止效果也很好。非常好——它们是一个很好的工具。祝你好运@Atomiton-谢谢,如果在我的答案中再次出现类似的内容,请随时编辑啊!我会。。。因为我相信减少评论噪音是一件好事。。。但我认为这是一个2000磅的动作。(当最低值提高时,我正处于1000磅的边缘)还有877k!
<asp:Label ID="lblStartTime" runat="server" Text='<%#Convert.ToDateTime(Eval("ExamStartTime")).ToShortTimeString()%>' />