Click here to Skip to main content
15,909,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
label5.Text = DateTime.Now.ToLongTimeString(); this would give a long value.

label6.Text = DateTime.Now.ToShortTimeString();
this would also give the same long value of time.how can we get a short time value(only the time value not the date)?

1/1/1900 3:34:00 AM would be the result for both label5 n label6
Posted
Updated 3-Jul-11 12:25pm
v4

There is no such thing as short or long time. To make a step further, I'll say there is no such thing as date vs time. (The type name DateTime is confusing, but this is due to tradition.) There is just time; everything else is related to its string representation. You need to work with time, not with its string representation. You should not do any calculations with string representation. You should only use it when you need to show a time string on screen of in text file.

Therefore, a single expression skipping intermediate DateTime value like DateTime.Now.ToLongTimeString is hardly useful anywhere.

A comprehensive ways of formatting time according to different UI cultures and any thinkable requirements is provided by the set of methods System.DateTime.ToString.

See:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

—SA
 
Share this answer
 
v3
Comments
Espen Harlinn 3-Jul-11 18:45pm    
NIce reply - my 5
Sergey Alexandrovich Kryukov 3-Jul-11 18:51pm    
Than you, Espen.
--SA
u can input the desired format yourself by creating a object of datetime function.

datatime obj= datetime.now;

string datentime;

datentime= obj.tostring(" HH:MM:SS "); //output is 7:30:55
("HH:MM tt"); /output is 7:30 pm
 
Share this answer
 
plz do check it again it gives only time : eg 6:51 AM
label1.Text = DateTime.Now.ToShortTimeString() ;
 
Share this answer
 
hi,

use tostring formatter
label5.Text = DateTime.Now.ToString("dd/MM/yyyy");
 
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