Q:

Storing current time without milliseconds component

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?

A:

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

A:

Simply subtract away the millisecond part:
DateTime myTime = DateTime.Now.TimeOfDay;
myTime = myTime.AddMilliseconds(-myTime.Millisecond);

It could be done in less code, without first assigning to myTime:
DateTime myTime = DateTime.Now.TimeOfDay.AddMilliseconds(
  -DateTime.Now.TimeOfDay.Millisecond);

Although somewhat elegant, it is a bad idea. When accessing TimeOfDay twice, there is a chance that it at some point will have passed another millisecond before the second access. In that case the result would not be zero milliseconds.

A:

I believe this is what you may be after:
TimeSpan timeNow = DateTime.Now.TimeOfDay;
TimeSpan trimmedTimeNow = new TimeSpan(timeNow.Hours, timeNow.Minutes, timeNow.Seconds);

