not displaying the fractional part of a timespan

B

Bob

Greetings All,

Is there a simple way of suppressing the fractional part of a timespan

At the moment I use one time span to populate another minus the fractional
part .. this is a bit clunky.

I'd love to inherit it an overload the ToString() method .. but I think its
a sealed class or some such in any case I had no joy there.

regards Sa
 
J

Jon Skeet [C# MVP]

Is there a simple way of suppressing the fractional part of a timespan

Which fractional part? Fractions of a day, fractions of an hour,
fractions of a minute, fractions of a second?
At the moment I use one time span to populate another minus the fractional
part .. this is a bit clunky.

Just put that in a utility method somewhere and it should be fine.
Once you're using C# 3 you can use extension methods to make it *look*
like it's part of the DateTime type :)

Jon
 
P

Peter Duniho

Bob said:
Greetings All,

Is there a simple way of suppressing the fractional part of a timespan

At the moment I use one time span to populate another minus the
fractional part .. this is a bit clunky.

There's no _good_ way to do it AFAIK. But what I have done is to use
the TimeSpan to create a DateTime instance, and then take advantage of
the string formatting that you can use for DateTime.

For example:

TimeSpan ts = ...;
string strTime = (new DateTime() + ts).ToString("h:mm:ss");

I think it's a little silly that the same formatting that can be applied
to DateTime doesn't work for TimeSpan, but it's not hard to trick .NET
into doing it anyway. :)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top