views:

391

answers:

3

Hi

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 without a simple if statement?

which is the cleanest way for that?

Thank you and best regards.

+1  A: 

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

Noon Silk
ok so you would write a function?
snarebold
it works, but have to convert the eval to datetime. thankyou.
snarebold
A: 

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.)

Guffa
A: 

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") %>
Heinzi