Results 1 to 9 of 9

Thread: [RESOLVED] TimeSpan format for total hours

  1. #1

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Resolved [RESOLVED] TimeSpan format for total hours

    Suppose I have a TimeSpan of 25 hours. If I format it as H:mm then I get "1:00". Is there a way to get "25:00"?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: TimeSpan format for total hours

    You're using the wrong format. The upper-case H is for the hour, using a 24-hour clock from 0 to 23. To get the hours from a TimeSpan get the property:
    Code:
    Dim ts As TimeSpan = New TimeSpan(0, 25, 0, 0)
    Console.WriteLine(ts.Hours.ToString())
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: TimeSpan format for total hours

    Perhaps,

    Code:
    Dim ts1 As New TimeSpan(1, 25, 61, 65)
    Debug.Print(String.Format("{0:00}:{1:00}", ts1.TotalHours, ts1.Minutes))

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: TimeSpan format for total hours

    A TimeSpan is NOT a DateTime. If the TimeSpan is longer than 24 hours there is a days property.

    Code:
            Dim ts1 As New TimeSpan(0, 25, 10, 22, 42)
    
            Dim s As String = ts1.ToString("d\ h\:mm\:ss\.ff")
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: TimeSpan format for total hours

    Thank you all. I'm sorry I wasn't clear enough. I need a format string. In this case the format is in a column of a DataGridView bound to DataTable. So I can't convert to string in any way. I need something like XX:mm where XX is the total number of hours in the TimeSpan.

    And I cant derive a string column for show either because users need to be able to type new values into it.

  6. #6
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: TimeSpan format for total hours

    Instead of a magical format string, handle the DataGridview.CellFormatting and DataGridview.CellParsing events. Here is a crude example with no error checking.

    Code:
       Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
          If e.ColumnIndex = 0 AndAlso Not IsDBNull(e.Value) Then
             Dim ts As TimeSpan = CType(e.Value, TimeSpan)
             e.Value = String.Format("{0:00}:{1:00}", Math.Floor(ts.TotalHours), ts.Minutes)
             e.FormattingApplied = True
          End If
       End Sub
    
       Private Sub DataGridView1_CellParsing(sender As Object, e As DataGridViewCellParsingEventArgs) Handles DataGridView1.CellParsing
          If e.ColumnIndex = 0 AndAlso Not IsDBNull(e.Value) Then
             Dim parts As String() = e.Value.ToString.Split(":"c)
             Dim hrs As Int32 = Int32.Parse(parts(0))
             Dim mins As Int32 = Int32.Parse(parts(1))
             e.Value = New TimeSpan(hrs, mins, 0)
             e.ParsingApplied = True
          End If
       End Sub

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: TimeSpan format for total hours

    People aren't misunderstanding. TimeSpan doesn't support printing hours more than 24. You can read the documentation if you don't trust me, all of the hour specifiers represent "total hours not counted as part of days". You'll have to do something like what TnTinMN displayed.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: TimeSpan format for total hours

    Quote Originally Posted by Sitten Spynne View Post
    People aren't misunderstanding. TimeSpan doesn't support printing hours more than 24. You can read the documentation if you don't trust me, all of the hour specifiers represent "total hours not counted as part of days". You'll have to do something like what TnTinMN displayed.
    that's partially true... it's the format specifier of the .ToString that doens't support it... and that's because it uses the .Hours value... BUT, TimeSpan DOES support hours greater than 24 through the TotalHours... it's just that is isn't exposed or used as part of the .ToString process... which means you need to use someother means to format it. TnTinTMN's solution is probably best at this point. Kind of a sucky solution, but it should work.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: TimeSpan format for total hours

    This is what I feared. Kind of dumb I think since TimeSpan would be useful for such things as time cards. A person is more likely to write 47:45 not 1.23:45 on their time card. On the bright side I was wondering if there was some mechanism like TnTinMN suggested. I looking forward to experimenting with that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width