Home All Groups Group Topic Archive Search About

Formatting timespan objects

Author
29 Mar 2005 10:06 AM
John Dann
I want to report a timespan value in a textbox on a form and thought I
might be able to do eg:

txtbox.text=format(timespan,"hh:mm:ss")

but this gives an invalid cast. Is there is any short way of achieving
this or do I need to extract and assemble each time element
separately?

Thanks
JGD

Author
29 Mar 2005 11:16 AM
Herfried K. Wagner [MVP]
"John Dann" <n***@prodata.co.uk> schrieb:
>I want to report a timespan value in a textbox on a form and thought I
> might be able to do eg:
>
> txtbox.text=format(timespan,"hh:mm:ss")
>
> but this gives an invalid cast. Is there is any short way of achieving
> this or do I need to extract and assemble each time element
> separately?

You may want to use 'String.Format' to concatenate the property values:

\\\
MsgBox( _
    String.Format( _
        "{0:00}:{1:00}:{2:00}", _
        ts.Hours, _
        ts.Minutes, _
        ts.Seconds _
    ) _
)
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
29 Mar 2005 11:27 AM
Ken Tucker [MVP]
Hi,

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemTimeSpanClassToStringTopic.asp

Ken
---------------
"John Dann" <n***@prodata.co.uk> wrote in message
news:vo9i4158kqhhecghda9mf00b25tp69caat@4ax.com...
I want to report a timespan value in a textbox on a form and thought I
might be able to do eg:

txtbox.text=format(timespan,"hh:mm:ss")

but this gives an invalid cast. Is there is any short way of achieving
this or do I need to extract and assemble each time element
separately?

Thanks
JGD
Author
29 Mar 2005 1:59 PM
Jay B. Harlow [MVP - Outlook]
John,
In addition to the other comments.

The "easiest" way to format a TimeSpan is to use the TimeSpan.ToString
method, which will return the results in the format: [-][d.]hh:mm:ss[.ff]

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemTimeSpanClassToStringTopic.asp

    Dim ts As TimeSpan
    txtbox.text= ts.ToString()

Remember the ToString method of "formattable" objects in .NET is normally
overridden to provide formatting for that object.

If you don't want the fractional seconds or days on the formatted string, I
normally convert the TimeSpan to a DateTime & then use custom DateTime
formatting.

Note TimeSpan itself only supports a fixed format, I will convert a TimeSpan
into a DateTime if I need custom formatting.

    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/default.asp?url=/library/en-us/cpguide/html/cpcondatetimeformatstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconformattingtypes.asp

Hope this helps
Jay


Show quoteHide quote
"John Dann" <n***@prodata.co.uk> wrote in message
news:vo9i4158kqhhecghda9mf00b25tp69caat@4ax.com...
>I want to report a timespan value in a textbox on a form and thought I
> might be able to do eg:
>
> txtbox.text=format(timespan,"hh:mm:ss")
>
> but this gives an invalid cast. Is there is any short way of achieving
> this or do I need to extract and assemble each time element
> separately?
>
> Thanks
> JGD