Results 1 to 11 of 11

Thread: Formatting time, (not that easy)

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Resolved Formatting time, (not that easy)

    Hi there VB Geniuses,

    I have a problem here which has been bugging me for a while.

    I have a list of time here but they are listed in seconds of the day.

    1.359375
    300.2813
    600.2422
    901.3594
    1200.117
    1500.219

    They are roughly at every 5mins intervals, how do i convert them into hh:mm:ss ?

    I can do it in excel but i am puzzled how to do it in vb

    I will watch out for my timely answer.

    Thanks !!!!
    Last edited by dinosaur_uk; Oct 27th, 2004 at 09:19 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    VB Code:
    1. Public Function GetTimeElapsed(ByVal intTimeStamp As Long) As DateTime
    2.  
    3.         If intTimeStamp > 86400 OrElse intTimeStamp < 1 Then
    4.             Return DateTime.Parse("00:00:00")
    5.  
    6.         Else
    7.  
    8.             Dim intSeconds As Int32
    9.             Dim intHH As Int32, intMM As Int32, intSS As Int32
    10.  
    11.  
    12.             intSeconds = Decimal.ToInt32(intTimeStamp)
    13.  
    14.             If intSeconds > 60 Then
    15.                 intSS = (intSeconds Mod 60)
    16.                 intMM = (intSeconds \ 60)
    17.  
    18.                 If intMM > 60 Then
    19.                     intHH = intMM \ 60
    20.                     intMM = intMM Mod 60
    21.  
    22.                 End If
    23.  
    24.             End If
    25.  
    26.             Dim dtTime As DateTime
    27.  
    28.             dtTime = DateTime.Parse(intHH.ToString & ":" & intMM & ":" & intSS)
    29.  
    30.             Return dtTime
    31.  
    32.  
    33.         End If
    34.  
    35.  
    36.     End Function

    To use it:

    MessageBox.Show(GetTimeElapsed(13122.383).ToLongTimeString)

  3. #3

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Oh my god ?

    Dear Mr Mendhak,

    How did you know that ? All Hail the VB Saint...

    The dinosaur is impressed.

  4. #4

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Oh no !!

    I have tried the code, and it seems to work up till it reaches 00:55:00 then it does not work ????

    it crashes

    An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

    Additional information: String was not recognized as a valid DateTime.

    And it highlights


    dtTime = DateTime.Parse(intHH.ToString & ":" & intMM & ":" & intSS)


  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    My mistake, try this:

    dtTime = DateTime.Parse(intHH.ToString & ":" & intMM.ToString & ":" & intSS.ToString)

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I tried with the erroneous code, still works fine. What input did you give?

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Hows this, I tried to make the code a bit smaller but I figured it would be enough as it is.

    VB Code:
    1. Public Function SecondsToDateTime(ByVal seconds As Double) As DateTime
    2.         Return New DateTime(seconds * TimeSpan.TicksPerSecond)
    3.     End Function

    :accurate:
    I don't live here any more.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Wooh.

    We have a new VB Angel.

  9. #9
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    Originally posted by mendhak
    Wooh.

    We have a new VB Angel.


    dont worry your would be a saint.

  10. #10

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    Amazing, the VB Angels having a cup of tea in VB Heaven, good stuff, i am impressed guys....

    I have learnt so much, (from someone who hates programming) and i am even finding it *more enjoyable...

    <catches some wild boar for VB Angels....>

  11. #11
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    I'll settle for VB Demon.
    I don't live here any more.

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