Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to display only seconds.... i dont want to display whole time...(hh:mm:ss)... i want only seconds..????? in my asp.net wep page .. how to get seconds from "DateTime.Now.ToLongTimeString()" pls help me :)..
Posted
Updated 20-Mar-13 3:52am
v2
Comments
Sergey Alexandrovich Kryukov 20-Mar-13 9:56am    
What prevents you from reading MSDN help pages properly? You would get the answer much faster.
—SA

Whatever the seconds are, this is hardly time; most likely, this is a TimeSpan:
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.totalseconds.aspx[^].

It's up to you how you get the time span, but most usually, this is a difference between two points of time:
C#
DateTime begin = //...
// ... time passes
DateTime end = //...
TimeSpan duration = end - pass; // prefer using operators rather then functions


If, by some weird reason, you need to show seconds of some time, you should understand that the seconds will cycle. You can show whatever you want, it's merely the matter of formatting and culture:
http://msdn.microsoft.com/en-us/library/ht77y576.aspx[^],
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^],
http://msdn.microsoft.com/en-us/library/8tfzyc64.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

My the main thing you need to learn is of course this: Microsoft Q209354.

—SA
 
Share this answer
 
Comments
fjdiewornncalwe 20-Mar-13 10:44am    
My 5.
Sergey Alexandrovich Kryukov 20-Mar-13 11:26am    
Thank you, Marcus.
—SA
Here you go :-

C#
DateTime dtNow = DateTime.Now;
CultureInfo cInfo = new CultureInfo("en-US");
DateTimeFormatInfo dtInfo = cInfo.DateTimeFormat;
dtInfo.ShortTimePattern = "ss";
yourlbl.Text = dtNow.ToString("t", cInfo);


Don't forget to add namespace using System.Globalization;

And If you want to downvote this answer please write the reason.

Good luck.
 
Share this answer
 
Comments
Joezer BH 21-Mar-13 4:00am    
5+
Raje_ 21-Mar-13 4:57am    
Thanks Edo Tzumer.
hi ram, try this !
This is the simplest form to get 'seconds' only
C#
yourlabel.Text=DateTime.Now.ToString("ss");
 
Share this answer
 
v2
Comments
Joezer BH 21-Mar-13 4:00am    
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