Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing current time without milliseconds component

Tags:

c#

datetime

I have a timespan object that needs to hold only time, without date. I would use

DateTime.Now.TimeOfDay

but the problem is it gives time in the format

15:51:51.7368329

I don't want the milliseconds component. How can I trim it out?

like image 866
xbonez Avatar asked Feb 09 '11 20:02

xbonez


People also ask

How do I remove milliseconds from a timestamp?

Use the setSeconds() method to remove the seconds and milliseconds from a date, e.g. date. setSeconds(0, 0) . The setSeconds method takes the seconds and milliseconds as parameters and sets the provided values on the date. Copied!

How do I remove seconds from DateTime?

Solution 1 Using different methods on the DateTime, like . Today give the date part, but the other parts of it are zero. This is by design. If you don't want to display the seconds when you create the string, use a format string, like MM/dd/yyyy hh.mm and leave the tt part off.


1 Answers

You can either use DateTime.Now.Hour/Minute/Second properties or you could use DateTime.Now.ToString("HH:mm:ss").

Refer here for more info: http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx

like image 158
Mohib Sheth Avatar answered Sep 18 '22 15:09

Mohib Sheth