Results 1 to 25 of 25

Thread: [RESOLVED] Vb6 elapsed time in days hours minutes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Resolved [RESOLVED] Vb6 elapsed time in days hours minutes

    I have the following code and i need lablel1.caption to show = day = .. / hours = .. / min = ..
    i dont know where to put days , hours , min
    here my code:

    Option Explicit
    Private mStartTime As Date
    Private Sub Form_Load()
    mStartTime = Now
    Timer1.Interval = 900
    End Sub
    Private Sub Timer1_Timer()
    Dim t As Date
    t = Now - mStartTime
    Label1.Caption = CStr(Int(CDbl(t))) & Format$(t, ":hh:mm:ss")
    End Sub

  2. #2
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Vb6 elapsed time in days hours minutes

    I would use:

    Code:
    Year(Date)
    Month(Date)
    Day(Date)
    Hour(Now)
    Minute(Now)
    Second(Now)
    to get the different numbers
    after that its simply easy to do what u need.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6 elapsed time in days hours minutes

    Quote Originally Posted by baka View Post
    I would use:

    Code:
    Year(Date)
    Month(Date)
    Day(Date)
    Hour(Now)
    Minute(Now)
    Second(Now)
    to get the different numbers
    after that its simply easy to do what u need.
    i need to start counting up with lablel1 to get how many days and hours and minutes passed from the moment i clicked command1
    could u plz give me the whole code so i can just copy paste
    thanx

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Vb6 elapsed time in days hours minutes

    could u plz give me the whole code so i can just copy paste
    we try not to do that here, we try to help you learn

    you have to integer divide seconds by 60 to get minutes, and minutes by 60 to get hours, keep the remainders from each division for the minutes and seconds to display
    where s is a number of seconds
    Code:
    m = s \ 60
    s = s Mod 60
    h = m \ 60
    m = m Mod 60
    MsgBox h & ":" & m & ":" & s
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6 elapsed time in days hours minutes

    Quote Originally Posted by westconn1 View Post
    we try not to do that here, we try to help you learn

    you have to integer divide seconds by 60 to get minutes, and minutes by 60 to get hours, keep the remainders from each division for the minutes and seconds to display
    where s is a number of seconds
    Code:
    m = s \ 60
    s = s Mod 60
    h = m \ 60
    m = m Mod 60
    MsgBox h & ":" & m & ":" & s
    Private Sub Form_Load()
    m = s \ 60
    s = s Mod 60
    h = m \ 60
    m = m Mod 60
    Label1.Caption = h & ":" & m & ":" & s

    End Sub
    it does work, i need to count and keep counting how many days hours minutes passed from the moment i click command 1

  6. #6
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Vb6 elapsed time in days hours minutes

    DateDiff can also be used, to compare two times.
    https://chennaiiq.com/developers/ref...s/datediff.asp

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6 elapsed time in days hours minutes

    Quote Originally Posted by baka View Post
    DateDiff can also be used, to compare two times.
    https://chennaiiq.com/developers/ref...s/datediff.asp
    i dont need to compare two dates
    i need count up timer, that counts keeps counting from the moment i press command1

  8. #8
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Vb6 elapsed time in days hours minutes

    the problem here is that u are talking about days/hours/minutes.
    if its just a counter u dont need to get the date/time at all,
    just set the timer to 1000 and increase a variable with 1 each loop,
    increase minute when second is 60 and reset it, increase hour when minute is 60 and reset it
    increase day when hour is 24 and reset it.

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Vb6 elapsed time in days hours minutes

    Set a date/time variable to Now in the Command click event.
    Then compare it with current date/time when you need it.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6 elapsed time in days hours minutes

    Quote Originally Posted by baka View Post
    the problem here is that u are talking about days/hours/minutes.
    if its just a counter u dont need to get the date/time at all,
    just set the timer to 1000 and increase a variable with 1 each loop,
    increase minute when second is 60 and reset it, increase hour when minute is 60 and reset it
    increase day when hour is 24 and reset it.
    give me code plz

  11. #11
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Vb6 elapsed time in days hours minutes

    We try to help you understand.
    We don’t work for you.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6 elapsed time in days hours minutes

    Quote Originally Posted by Arnoutdv View Post
    We try to help you understand.
    We don’t work for you.
    i know im just newbie + lazy
    i been searching for hours now for code that solves my problem
    still cant find
    that is why i ask here for help
    thanx in advance

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6 elapsed time in days hours minutes

    Option Explicit
    Private mStartTime As Date
    Private Sub Form_Load()
    mStartTime = Now
    Timer1.Interval = 900
    End Sub
    Private Sub Timer1_Timer()
    Dim t As Date
    t = Now - mStartTime

    Label1.Caption = CStr(Int(CDbl(t))) & " days " & Format$(t, "hh:mm")

    this code shows only days = ..
    i need to show hours = ..
    and min = ...

  14. #14
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Vb6 elapsed time in days hours minutes


  15. #15
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Vb6 elapsed time in days hours minutes


  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6 elapsed time in days hours minutes

    Quote Originally Posted by jdc2000 View Post
    it does not show how many days

  17. #17
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Vb6 elapsed time in days hours minutes

    is this a homework assignment?

    Or are you creating a REAL project for use in the world?
    Sam I am (as well as Confused at times).

  18. #18
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Vb6 elapsed time in days hours minutes

    Well, I may or may not offer some advice (as if I could anyway---I'm just a novice at VB6 as well).

    But I AM curious...if you have not allowed this to run for a day or more, of course it will not (YET) count days!!!!!!!

    How are you determining if days are being counted or not.
    Sam I am (as well as Confused at times).

  19. #19
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Vb6 elapsed time in days hours minutes

    it does not show how many days
    So, change it so it does. Not that anyone is actually going to be watching a progress meter for days.

    If you are actually wanting to create a clock program, there are different methods for that. Maybe if you explained exactly what you are trying to achieve, in detail, we might have some additional ideas.

  20. #20
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Vb6 elapsed time in days hours minutes

    Quote Originally Posted by geekmaro View Post
    Dim t As Date
    t = Now - mStartTime

    Label1.Caption = CStr(Int(CDbl(t))) & " days " & Format$(t, "hh:mm")
    This Code-Snippet of yours above is already doing "well enough" I think -
    the only thing that's missing (knowledgewise) is, to force the Format-Function to:
    - not react to special chars
    - instead showing them as "normal text" (in-between the formatted-parts)

    And that's called "escaping of characters" (usually by prefixing them with the BackSlash-Char \)

    Here is an example, how such an extended Format-String could look like in your case:

    Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n | s \s\e\c"

    I've marked the "active Format-Special-Chars" bold in the above string-Const -
    and each Char of the "non-active-word-parts" (days hours min sec) was escaped with a BackSlash-Char.

    Here is your basic example again (slightly changed, now making use of the above "extended Format-String-Const"):
    Code:
    Option Explicit
    
    Private mStartTime As Date, WithEvents tmrDelta As VB.Timer
    
    Private Sub Form_Load()
      mStartTime = Now
      Set tmrDelta = Controls.Add("VB.Timer", "tmrDelta")
          tmrDelta.Interval = 500
    End Sub
    
    Private Sub tmrDelta_Timer()
      Me.Caption = GetElapsedTimeString(Now - mStartTime)
    End Sub
    
    Function GetElapsedTimeString(ByVal dT As Double) As String
      Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n | s \s\e\c"
      
      GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
    End Function
    Olaf

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    224

    Re: Vb6 elapsed time in days hours minutes

    Quote Originally Posted by Schmidt View Post
    This Code-Snippet of yours above is already doing "well enough" I think -
    the only thing that's missing (knowledgewise) is, to force the Format-Function to:
    - not react to special chars
    - instead showing them as "normal text" (in-between the formatted-parts)

    And that's called "escaping of characters" (usually by prefixing them with the BackSlash-Char \)

    Here is an example, how such an extended Format-String could look like in your case:

    Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n | s \s\e\c"

    I've marked the "active Format-Special-Chars" bold in the above string-Const -
    and each Char of the "non-active-word-parts" (days hours min sec) was escaped with a BackSlash-Char.

    Here is your basic example again (slightly changed, now making use of the above "extended Format-String-Const"):
    Code:
    Option Explicit
    
    Private mStartTime As Date, WithEvents tmrDelta As VB.Timer
    
    Private Sub Form_Load()
      mStartTime = Now
      Set tmrDelta = Controls.Add("VB.Timer", "tmrDelta")
          tmrDelta.Interval = 500
    End Sub
    
    Private Sub tmrDelta_Timer()
      Me.Caption = GetElapsedTimeString(Now - mStartTime)
    End Sub
    
    Function GetElapsedTimeString(ByVal dT As Double) As String
      Const sFmt$ = " \d\a\y\s | h \h\o\u\r\s | m \m\i\n | s \s\e\c"
      
      GetElapsedTimeString = Int(dT) & Format$(dT, sFmt)
    End Function
    Olaf
    thanks alot
    it works like a charm

  22. #22
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Vb6 elapsed time in days hours minutes

    Label1.Caption = CStr(Int(CDbl(t))) & " days " & Format$(t, "hh:mm")

    this code shows only days = ..
    i need to show hours = ..
    and min = ...
    of course, as you are converting the double to integer no time part will be kept
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  23. #23
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: [RESOLVED] Vb6 elapsed time in days hours minutes

    Quote Originally Posted by westconn1 View Post
    of course, as you are converting the double to integer no time part will be kept
    No, there was nothing wrong in his original expression (which holds the TimeDiff in a Date-Variable t):

    Label1.Caption = CStr(Int(CDbl(t))) & " days " & Format$(t, "hh:mm")

    ...or a bit less noisy (since CStr is implicitely applied on an expression which is part of a String-Concatenation):

    Label1.Caption = Int(CDbl(t)) & " days " & Format$(t, "hh:mm")

    The above will (after some time) reliably produce Label.Caption Strings like e.g.:

    "3 days 07:33"

    What he meant with "hours and min are missing" (not expressing himself well enough) was simply:
    "how to include additional Unit-Specifiers like "hours" or "min" within a Format-String".

    Olaf

  24. #24
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: [RESOLVED] Vb6 elapsed time in days hours minutes

    Quote Originally Posted by Schmidt View Post
    Label1.Caption = Int(CDbl(t)) & " days " & Format$(t, "hh:mm")
    I think just Int(t) (without the CDbl conversion) should also work (not tested).

  25. #25
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: [RESOLVED] Vb6 elapsed time in days hours minutes

    Quote Originally Posted by Eduardo- View Post
    I think just Int(t) (without the CDbl conversion) should also work (not tested).
    Not when t is of type Date (since Int returns Variant, and tries to preserve the Input-Type).

    Olaf

Tags for this Thread

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