Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Iam trying to display only 'time' from DateTime.So that my gridview will looks simpler.Because I have to display it in the aspx page.I tried some but they are not working.

C#
DateTime.Parse(DateTime.Now.ToString("hh:mm:ss"))


The code above is not working.Can anyone suggest a better way?
Posted
Updated 1-Jan-13 1:14am
v2

Hi,

Try this:
C#
string time = DateTime.Now.ToLongTimeString();

Hope this helps.
 
Share this answer
 
C#
string time = DateTime.Now.TimeOfDay.ToString();
 
Share this answer
 
v2
Comments
danil33 1-Jan-13 7:28am    
Now also date is showing.I don't know what is the problem.

I'll copy that code completely

ConsumptionGrid.DataSource = Utilities._dbManager.GetConsumptionlist(PageIndex,
Const.NumberOfItemsPerPage,
_consumer.MeterNumber,
DateTime.Parse(DateTime.Now.ToLongTimeString()));
ConsumptionGrid.DataBind();
[no name] 1-Jan-13 9:23am    
dont parse it into datetime, you are converting the time string again into a variable which contain date. use the following

ConsumptionGrid.DataSource = Utilities._dbManager.GetConsumptionlist(PageIndex,
Const.NumberOfItemsPerPage,
_consumer.MeterNumber,
DateTime.Now.ToLongTimeString());
danil33 1-Jan-13 23:58pm    
Hi,
I parsed it because it is showing Error:Argument type 'string' is not assignable to parameter type 'System.DateTime'
do you want something like this
C#
DateTime.Now.ToString("hh:mm:ss")
 
Share this answer
 
Comments
danil33 1-Jan-13 7:47am    
Hi Satish,
I tried this but again it is giving both date and time.
satishgv1985 1-Jan-13 8:09am    
I just tried this
DateTime.Now.ToString("hh:mm:ss")
in my LinqPad and I got
06:37:53
post your code for more details
satishgv1985 1-Jan-13 8:10am    
one more, have you used any formatting options in your aspx code. Post that code as well to see indetail
Hi,

Try this :-
VB
a) Long time pattern
DateTime.Now.ToString("T")

b) Short time pattern
DateTime.Now.ToString("t")
 
Share this answer
 
try this.
C#
DateTime Time = DateTime.Now;
Time.ToString("h tt");
 
Share this answer
 

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