Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

My database will return the time in the format of 24hrs as follows.

2012-04-10T21:30:00

But i need to show only timing in GridView as follows

Retrieving Format Required format
2012-04-10T21:30:00 9:30PM
2012-04-10T09:30:00 9:30AM

I need to extract only timing from this & i need to show in GridView as specified format.

How can i achieve this?
Posted

Try this:
C#
RetrievedValue.ToString("hh:mm tt");

And hey you can always bookmark this URL for reference - Custom Date and Time Format Strings[^]

Hope this helps!
 
Share this answer
 
v3
Comments
VJ Reddy 10-Apr-12 11:15am    
Good link. +5
sjelen 10-Apr-12 11:37am    
Edited: was "hh:ss tt", should be "hh:mm tt"
Ankur\m/ 11-Apr-12 2:56am    
Thanks!
If you're using BoundField, DynamicField or similar, just set DataFormatString property to "hh:mm tt".

If you're using TemplateField and Eval() or Bind() you can pass format string as second parameter to them:
C#
Eval("Date", "hh:mm tt")


Of course, this will work only if you return DateTime from database.
It won't work if you return it as string.
 
Share this answer
 
v2
Comments
Ankur\m/ 11-Apr-12 2:57am    
Precise, 5!

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