views:

323

answers:

5

In an ASP.NET page I have this:

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

I'd like to have it formatted like

... Eval("MyDateTime", "{0:d}") ... // Display only the date

if and only if the time part of MyDateTime is 00:00:00. Otherwise like this:

... Eval("MyDateTime", "{0:g}") ... // Display date and time in hh:mm format

Is this possible and how can I do that?

Thank you for hints in advance!

A: 

I am not sure if you are looking for this, but I feel its worth trying. Hope it works

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

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

Krishna
+1  A: 

didn't test, but off the top of my head:

in markup

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

in code-behind:

protected string FormatMyDateTime(DateTime date)
{
      // Do your if else for formatting here.
}
nickyt
Thanks, it works fine, except in markup "...as DateTime" needs to be replaced by "...as DateTime?" (also the DateTime parameter in code-behind must be DateTime?) because the "as" operator requires either a reference type or a nullable type (the debugger was telling me).
Slauma
Well, if `MyDateTime` is a `DateTime`, the correct way would be to use a "regular" cast (`(DateTime)Eval("MyDateTime")`) instead of `as`.
Heinzi
The `as` operator cannot be used with `DateTime` because it's a non-nullable value type.
João Angelo
@João - Thanks for pointing that out... like I said, "didn't test, but off the top of my head:" ;)
nickyt
@Heinzi: In my project MyDateTime can be nullable and non-nullable as well. So in the non-nullable case I would use your proposal '(DateTime)Eval("MyDateTime")', but for nullable datetimes 'Eval("MyDateTime") as DateTime?'. The method in code-behind I need only once as "FormatMyDateTime(DateTime? datetime)". It works with nullable datetime and non-nullable datetime too. (I have to catch the null case in the method then before formatting of course). Is this the way to go?
Slauma
@Slauma: Sounds good to me. BTW, it works because `DateTime` can be implicitly converted to `DateTime?`.
Heinzi
OK, thank you!By the way and off-topic: How do you all mark code with that grey background here in the comments?
Slauma
+6  A: 

I'd put this in my code-behind:

// 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");
    }
}

And change the .aspx to call that:

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

If you do this in multiple places, consider writing an extension method for DateTime and put this logic there (perhaps with additional parameters to supply different formats, etc.).

Jeff Sternal
Thanks for the link to "extension methods". I don't know what this is (except I've heard the term), I'll take a look. Actually yes, I'd like to use this DateTime format on several places and in several pages. I've placed the code into a tool namespace and static class. Works also fine so far.
Slauma
Excellent - they're a great tool. Good luck!
Jeff Sternal
Small correction, you forgot the return type: protected String FormatDateHid...
Atømix
@Atomiton - thank you, and feel free to edit stuff like that right in, if it ever comes up again in my answers. :)
Jeff Sternal
Argh! I would have... as I believe the reduction of comment noise is a good thing... but I think that's a 2000pt action. ( I was on the verge of 1000pt when the minimum was upped ) 877k to go!
Atømix
A: 

You can substitute the following code in the aspx file or create a method and call the method to return the value.

<%
   DateTime dtTime = DateTime.Now;

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

You did not mention which .net language you use. With VB.NET, you can use the following inline expression:

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

I did not test with C#, but I guess replacing If(...) with the ternary ?: operator and casting the result of Eval to a DateTime before accessing TimeOfDay should do the trick.

Heinzi
I'm using C#. Yes, I guess, the ?: would work.
Slauma