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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Should be like…
Text='<%# Bind("RequiredDate", "{0:MMM dd, yyyy}") %>'
Method 2
This should work
Text='<%# Bind("DateOfBirth", "{0:MMM dd, yyyy}") %>'
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0