Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2007

Tip: Looking for answers? Try searching our database.

TimeSpace.Parse() usage

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SpaceMarine - 15 Aug 2007 18:53 GMT
hello,

ive read the MSDN docs on using the TimeSpace.Pars() method to format
timespans, however i found their examples unclear.

currently i am doing this:

    Dim startTime As DateTime = DateTime.Now
    ...
    Dim endTime As DateTime = DateTime.Now
    Dim elapsedTime As TimeSpan = endTime.Subtract(startTime)

    writer.WriteLine("Time difference: {0} hours, {1} minutes, {2}
seconds.", elapsedTime.Hours, elapsedTime.Minutes,
elapsedTime.Seconds)

...but i get the impression there is a much simpler string formatter
technique. ive tried this, to no effect:

    writer.WriteLine("Time difference: {0:hh:mm:ss}", elapsedTime)

anybody know the magic?

thanks!
sm
Peter Duniho - 15 Aug 2007 19:02 GMT
> [...]
> ....but i get the impression there is a much simpler string formatter
[quoted text clipped - 3 lines]
>
> anybody know the magic?

There's no magic.  The TimeSpan class has no formatting options, so you
need to break it apart as in the example you found, if you are going to
use the TimeSpan directly.

One alternative is to convert the TimeSpan back to a DateTime, which of
course does have formatting options like you're trying to use:

    writer.WriteLine("Time difference: {0:hh:mm:ss}",
        new DateTime() + elapsedTime);

Sort of hacky, but it works.

Pete
SpaceMarine - 15 Aug 2007 20:20 GMT
> > [...]
> > ....but i get the impression there is a much simpler string formatter
[quoted text clipped - 5 lines]
>
> There's no magic.  The TimeSpan class has no formatting options

bummer. i had thought from all that jibjab on msdn's TimeSpan.Parse()
that there would be something like it.

http://msdn2.microsoft.com/en-us/library/system.timespan.parse(vs.71).aspx

thanks
sm
Peter Duniho - 15 Aug 2007 21:11 GMT
>> There's no magic.  The TimeSpan class has no formatting options
>
> bummer. i had thought from all that jibjab on msdn's TimeSpan.Parse()
> that there would be something like it.

Yes.  You'd think that you would be able to specify string formatting
for converting a TimeSpan to a string that's similar to the description
they offer for parsing from a string.

But as far as I know, they don't.  And I have looked pretty thoroughly.
 The TimeSpan.ToString() method doesn't even offer an overload with a
formatting string parameter.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.