How to Format a TimeSpan Without the Decimal Part of the Seconds

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

If I display a TimeSpan I get something like

00:05:17.6217891

when what I would like to see is

00:05:18

Is there an easy way to get this output? Try as I might I just can't find
it.

TIA

Charles
 
Here's my thought....

dim TS as String = <GetTimeSpan>.ToString
TS=TS.Substring(0,LastIndexOf(":")+2)
 
Charles,
What I normally do when I need custom formatting of a TimeSpan is "convert"
it to a DateTime, then use custom DateTime formatting. Something like:

Dim ts As TimeSpan
Dim dt As DateTime = DateTime.MinValue.Add(ts)
Dim s As String

s = ts.ToString() ' default TimeSpan formatting
s = dt.ToString("H:mm:ss") ' custom DateTime formatting


For details on custom datetime formats see:

http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| If I display a TimeSpan I get something like
|
| 00:05:17.6217891
|
| when what I would like to see is
|
| 00:05:18
|
| Is there an easy way to get this output? Try as I might I just can't find
| it.
|
| TIA
|
| Charles
|
|
 
Charles,

In addition to the others,

I concatinate the individual parts with the ":" between them.

Hours:Minutes:Seconds

If I am lazy it is

dim str as string = ts.ToString.length(8)

(not so nice as documentation)

I hope this helps,

Cor
 
Hi Jay

Thanks for the reply. As I feared it is not a one-liner. With all the other
formatting options, is this an omission, in your view, that TimeSpan does
not have ToString that can take a format?

Charles
 
Hi Terry

Thanks. I can see that this would work, but I think it confirms that there
is no particularly elegant solution in the way that other formatting works.

Charles
 
Hi Cor

This is what I was resigned to doing in the end. I suppose I should write a
helper function so that I can call it from anywhere, or use Jay's suggestion
to add more formatting options.

Incidentally,
dim str as string = ts.ToString.length(8)

gives me a syntax error. Is that what you meant or am I taking it too
literally?

Charles
 
Charles,

I typed this in the message, really a stupid error, that VB helps me with
this is the main reason that I like VBNet above C#.

ts.ToString.Substring(0,8)

as well not checked however probably better.

You knew this...............

:-))

Cor
 
Jay,

Jay B. Harlow said:
What I normally do when I need custom formatting of a TimeSpan is
"convert"
it to a DateTime, then use custom DateTime formatting. Something like:

That's what I do too, but I am still wondering why 'TimeSpan' doesn't have
an overloaded parameterized version of 'ToString' which takes the format
string, not even in .NET 2.0.
 
Charles,
IMHO Unfortunately it seems that TimeSpan is the "abandoned child" of the
Framework (a.k.a an omission or oversight of the Framework designers). In
that:

- TimeSpan does not implement IConvertible, nor does IConvertible support
TimeSpan (does not have a ToTimeSpan). Along with IConvertible there should
be a TypeCode.TimeSpan
- TimeSpan does not implement IFormattable & have custom formatting similar
to the "Time" part of DateTime.


If TimeSpan was IConvertible friendly then you could use Convert.ToString &
Convert.ToTimeSpan to convert Timespans to & from other types.

If TimeSpan was IFormattable friendly then it would support custom
formatting.

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi Jay
|
| Thanks for the reply. As I feared it is not a one-liner. With all the
other
| formatting options, is this an omission, in your view, that TimeSpan does
| not have ToString that can take a format?
|
| Charles
|
|
| message | > Charles,
| > What I normally do when I need custom formatting of a TimeSpan is
| > "convert"
| > it to a DateTime, then use custom DateTime formatting. Something like:
| >
| > Dim ts As TimeSpan
| > Dim dt As DateTime = DateTime.MinValue.Add(ts)
| > Dim s As String
| >
| > s = ts.ToString() ' default TimeSpan formatting
| > s = dt.ToString("H:mm:ss") ' custom DateTime formatting
| >
| >
| > For details on custom datetime formats see:
| >
| >
http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp
| >
| > For information on formatting in .NET in general see:
| >
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp
| >
| > --
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > | > | If I display a TimeSpan I get something like
| > |
| > | 00:05:17.6217891
| > |
| > | when what I would like to see is
| > |
| > | 00:05:18
| > |
| > | Is there an easy way to get this output? Try as I might I just can't
| > find
| > | it.
| > |
| > | TIA
| > |
| > | Charles
| > |
| > |
| >
| >
|
|
 
Jay,
Charles,
IMHO Unfortunately it seems that TimeSpan is the "abandoned child" of the
Framework (a.k.a an omission or oversight of the Framework designers). In
that:

- TimeSpan does not implement IConvertible, nor does IConvertible support
TimeSpan (does not have a ToTimeSpan). Along with IConvertible there
should
be a TypeCode.TimeSpan
- TimeSpan does not implement IFormattable & have custom formatting
similar
to the "Time" part of DateTime.
I became already nervous when I saw this message because that message I had
sent from the AdoNet newsgroup.

:-)

Cor
 

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

Back
Top