10

is there a way that I can format a Date binded in a ListView?

I have this snippet of ListView

<ListView ID="lvView" runat="server">
    <ItemTemplate>
        //... some bounded data
        <asp:Label ID="lblDate" runat="server" Text='<%# Bind("RequiredDate") %>' />
        //... another bounded data
    </ItemTemplate>
</ListView>

Since RequiredDate is a DateTime it will display somethine like this 10/20/2010 11:08:55 AM

What I want is to Format that date to output something like this Oct. 20, 2010. Normally if it is a DateTime I can write something like this requiredDate.ToString("MMMM dd, yyyy") but inside the ListView binded data I cannot do that.

I don't want to use OnItemDatabound. I just want it to be formatted inline. Is this possible?

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
rob waminal
  • 18,117
  • 17
  • 50
  • 64

2 Answers2

27

Should be like...

Text='<%# Bind("RequiredDate", "{0:MMM dd, yyyy}") %>'
Eonasdan
  • 7,563
  • 8
  • 55
  • 82
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • 1
    I have tried this → Text='<%# Bind("Date", "{0:MM/dd/yyyy hh:mm:ss tt}") %>'> ... But still get this format: "3/1/2012 1:00:00 PM". What am I doing wrong??? Thanks! – daveomcd Dec 15 '11 at 17:30
4

This should work

Text='<%# Bind("DateOfBirth", "{0:MMM dd, yyyy}") %>'
Ashaar
  • 467
  • 1
  • 4
  • 15