By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,536 Members | 1,504 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,536 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Here's my thought....

dim TS as String = <GetTimeSpan>.ToString
TS=TS.Substring(0,LastIndexOf(":")+2)

"Charles Law" <bl***@nowhere.com> wrote in message
news:ef**************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #2

P: n/a
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/de...matstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/de...ttingtypes.asp

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Charles Law" <bl***@nowhere.com> wrote in message
news:ef**************@TK2MSFTNGP15.phx.gbl...
| 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
|
|
Nov 21 '05 #3

P: n/a
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
Nov 21 '05 #4

P: n/a
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:e7**************@TK2MSFTNGP14.phx.gbl...
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/de...matstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/de...ttingtypes.asp

--
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Charles Law" <bl***@nowhere.com> wrote in message
news:ef**************@TK2MSFTNGP15.phx.gbl...
| 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
|
|

Nov 21 '05 #5

P: n/a
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
"Terry Olsen" <to******@hotmail.com> wrote in message
news:er**************@tk2msftngp13.phx.gbl...
Here's my thought....

dim TS as String = <GetTimeSpan>.ToString
TS=TS.Substring(0,LastIndexOf(":")+2)

"Charles Law" <bl***@nowhere.com> wrote in message
news:ef**************@TK2MSFTNGP15.phx.gbl...
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


Nov 21 '05 #6

P: n/a
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
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:O$**************@TK2MSFTNGP14.phx.gbl... 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

Nov 21 '05 #7

P: n/a
Charles,
dim str as string = ts.ToString.length(8)


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
Nov 21 '05 #8

P: n/a
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9

P: n/a
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
"Charles Law" <bl***@nowhere.com> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
| 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
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
| message news:e7**************@TK2MSFTNGP14.phx.gbl...
| > 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/de...matstrings.asp
| >
| > For information on formatting in .NET in general see:
| >
http://msdn.microsoft.com/library/de...ttingtypes.asp
| >
| > --
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Charles Law" <bl***@nowhere.com> wrote in message
| > news:ef**************@TK2MSFTNGP15.phx.gbl...
| > | 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
| > |
| > |
| >
| >
|
|
Nov 21 '05 #10

P: n/a
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
Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.