开发者

format datetime - format a datetime with or without the time

Sometimes I get a datetime with a time sometimes it's just the date.

Of course if it's just the date I I want to format with "dd.MM.yyyy" and if it has a time "dd.MM.yyyy HH:mm"..

This is in a repeater, so I thought may be it's possible w开发者_开发问答ithout a simple if statement?

which is the cleanest way for that?

Thank you and best regards.


Perhaps try checking if theDateTime.TimeOfDay.TotalSeconds is > 0


A DateTime value always has a time component, even if the time is 00:00:00. If you want a special format for the case when the time is zero, you have to make a comparison:

<%# TheDate.ToString(TheDate.TimeOfDay.TotalSeconds == 0 ? "dd'.'MM'.'yyyy" : "dd'.'MM'.'yyyy HH:mm:ss")%>

(I assume that you want the hours in the format also, not just minutes and seconds.)


A datetime always contains a time component, so I guess you want to display just the date if the time is "00:00" (12:00 AM).

As far as I know, you cannot do this just with format strings, so you will have to resort to using some (simple) function. You can, however, use the ternary operator in ASP.NET (untested). In C#:

<%# checkIfTimeIsEmpty ? Eval(Container.DataItem, "myDate", "someformat") : Eval(Container.DataItem, "myDate", "someOtherFormat") %>

or in VB.NET:

<%# If(checkIfTimeIsEmpty, Eval(Container.DataItem, "myDate", "someformat"), Eval(Container.DataItem, "myDate", "someOtherFormat") %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜