Sometimes I get a datetime with a time sometimes it's just the date.
Of course if it's just the date II 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 without a simple if statement?
which is the cleanest way for that?
Thank you and best regards.
也许尝试检查theDateTime.TimeOfDay.TotalSeconds
是否> 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") %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.