Dania
Dania

Reputation: 1688

Getting incorrect date format when retrieving data from SQL in asp.net

I have a database which has date field and time field. I want to retrieve these two values from the DB and display them in a Label. I could do this by binding an SqlDataSource with the DB and using Eval in the label, but the problem is that I always get an extra 12:00:00 am when retrieving the date. For example if the date is 13/11/2015, when I retrieve it via asp.net I get, 13/11/2015 12:00:00 am.

I have executed a query in SQL itself and the results where correct with no 12:00:00 am. Also, when I configured the 'SqlDataSource' I clicked Test Query and the results were correct.

I couldn't figure out why I'm getting incorrect format when displaying it in a label. The label is placed inside a repeater ItemTemplate. All other retrived data from the DB are retrieved correctly from inside the repeater except date. Here is the code which places the date in the Label,

<span class="date_time_lbl">posted at: </span>
<asp:Label runat="server" Text='<%#Eval("tDate") %>'/>
<span> </span>
<asp:Label runat="server" Text='<%#Eval("Time") %>'/>

Can anyone please tell me what is causing this problem?

Upvotes: 0

Views: 116

Answers (1)

ercyon
ercyon

Reputation: 111

you can use formating in eval as a second parameter;

Eval("Date", "{0:d}")

for standard format options: https://msdn.microsoft.com/library/az4se3k1(v=vs.100).aspx

for custom options: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

Upvotes: 2

Related Questions