Results 1 to 9 of 9

Thread: Function for returing Time

  1. #1

    Thread Starter
    Addicted Member hyousuf2's Avatar
    Join Date
    Dec 2004
    Location
    Dublin
    Posts
    226

    Function for returing Time

    hi ihave made a custom fucntion in my application which takes millisec as argument and returns time in this format hh:mm:ss, but it doesn't work perfectly. here is the code

    VB Code:
    1. Private Function GetDuration(time as Long)
    2.  
    3.     Dim h, m, s  
    4.    
    5.     time = time / 1000 'Convert it to seconds
    6.    
    7.     'Now Convert the time into Standard Format
    8.         'Hours
    9.         h = Int(time / 3600)
    10.         'Minutes
    11.         m = time / 3600 Mod 60
    12.         'Seconds
    13.         s = time - (h * 3600) - (m * 60)
    14.        
    15.     'Convert it to Format hh:mm:ss
    16.     GetDuration = Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00")
    End Function

    in my application i run a timer every second which updates the time n the above function has to perform just like a digital clock, but the problem is that this function will just add seconds, n hours n minutes will always remain 0

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Function for returing Time

    Why not just use the Format and Time functions? Since your time format's smallest identifier is seconds
    there is no need to do anything smaller.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member hyousuf2's Avatar
    Join Date
    Dec 2004
    Location
    Dublin
    Posts
    226

    Re: Function for returing Time

    well then pls tell me those time functions as i dunno ne of them

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Function for returing Time

    This will give you the time and output it correctly.

    VB Code:
    1. Time.Text = Format(Now, "hh:mm:ss")

  5. #5

    Thread Starter
    Addicted Member hyousuf2's Avatar
    Join Date
    Dec 2004
    Location
    Dublin
    Posts
    226

    Re: Function for returing Time

    well am afraid things are not that simple, i get milliseond time n i have to display it hh:mm:ss format, n neva da less i would prefer correcting the function i wrote

  6. #6
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Function for returing Time

    Try:
    VB Code:
    1. Private Function GetDuration(time As Long) As String
    2.  
    3.     Dim h As Long, m As Long, s As Long
    4.    
    5.     time = time / 1000 'Convert it to seconds
    6.    
    7.     'Now Convert the time into Standard Format
    8.    
    9.     'Hours
    10.     h = time \ 3600
    11.     'Minutes
    12.     time = time - (h * 3600)
    13.     m = time \ 60
    14.     'Seconds
    15.     time = time - (m * 60)
    16.     s = time
    17.    
    18.     'Convert it to Format hh:mm:ss
    19.     GetDuration = Format$(h, "00") & ":" & Format$(m, "00") & ":" & Format$(s, "00")
    20.  
    21. End Function
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  7. #7

    Thread Starter
    Addicted Member hyousuf2's Avatar
    Join Date
    Dec 2004
    Location
    Dublin
    Posts
    226

    Re: Function for returing Time

    well actually i managed to rectify my code myself, but thnkx for the help provided pnish !

  8. #8

    Thread Starter
    Addicted Member hyousuf2's Avatar
    Join Date
    Dec 2004
    Location
    Dublin
    Posts
    226

    Re: Function for returing Time

    it looks something like this !

    VB Code:
    1. Private Function GetDuration(st As RAS_STATS)
    2.     Dim h, m, s
    3.    
    4.     Time = st.dwConnectDuration 'Get the time in milli seconds
    5.    
    6.     Time = Time / 1000 'Convert it to seconds
    7.    
    8.     'Now Convert the time into Standard Format
    9.         'Hours
    10.         h = Int(Time / 3600)
    11.         'Minutes
    12.         m = Int((Time Mod 3600) / 60)
    13.         'Text2 = Int(m)
    14.         'Seconds
    15.         s = Time - (h * 3600) - (m * 60)
    16.         'Text2 = time - (h * 3600) - Int((m * 60))
    17.        
    18.     'Convert it to Format hh:mm:ss
    19.     'GetDuration = h & ":" & m & ":" & s
    20.     GetDuration = Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00")
    21. End Function

  9. #9
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Function for returing Time

    Quote Originally Posted by hyousuf2
    well actually i managed to rectify my code myself, but thnkx for the help provided pnish !
    Well done. And BTW you're welcome.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

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