Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi frnds,

am working on asp.net c#.

I want to display date using label.

presently am display like this 6/22/2013 5:15:38 pm

So i need like this 22/6/2013 5:15:38 pm

this is my code.................

lblDateTime.Text = reader[7].ToString();

Please help thanks in advance.
Posted
Updated 12-Nov-17 22:28pm

Hi,

Try:
C#
if (reader[7] is DateTime)
{
lblDateTime.Text = (reader[7] as DateTime).ToString("dd/MM/yyyy h:mm:ss tt");
}
else
{
    // reader[7] isn't a DateTime
}

Hope this helps.
 
Share this answer
 
Comments
Ron Beyer 22-Jun-13 8:39am    
+5
Thomas Daniels 22-Jun-13 8:54am    
Thank you!
Tiwari Avinash 26-Jun-13 2:04am    
MY 5 sir ProgramFOX!!
Thomas Daniels 26-Jun-13 2:12am    
Thank you!
Try This ...:)
C#
lblDate.Text=Convert.ToDateTime(DateVariable).ToString("dd/MM/yyyy");
 
Share this answer
 
v2
You can also convert in procedure like

convert(datetime,FieldName) as FieldName

Thanks
 
Share this answer
 
Comments
CHill60 26-Jun-13 8:55am    
That will only work if the OPs "reader" is for something that supports tSql
[no name] 26-Jun-13 23:43pm    
Try this

select convert(varchar,getdate(),113)
select convert(varchar,getdate(),103)
Use String.Format("DD/MM/YYYY")
 
Share this answer
 
v2
Try this :

C#
lbl.Text = Convert.ToDateTime(YourDateVariable).ToString("dd/MM/yyyy");
 
Share this answer
 
Comments
Ron Beyer 22-Jun-13 8:39am    
If the value in the column is DbNull, this will cause an exception. ProgramFox's is the most elegant, otherwise you also need to check for DB null and that the type is convertible to a DateTime before attempting the conversion.
Dineshshp 22-Jun-13 8:56am    
I Know very well & providing only solution for this problem. Error tracking is responsibility of programmer not helper...
Ron Beyer 22-Jun-13 9:43am    
So you assume that somebody who doesn't know how to format a date knows how to correctly handle DbNull and type conversion checking? Its at least worth mentioning by the "helper" that the code above can throw errors in common situations...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900