-
Jan 6th, 2021, 02:28 PM
#1
Thread Starter
Addicted Member
[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
-
Jan 6th, 2021, 02:50 PM
#2
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.
-
Jan 6th, 2021, 02:54 PM
#3
Thread Starter
Addicted Member
Re: Vb6 elapsed time in days hours minutes
Originally Posted by baka
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
-
Jan 6th, 2021, 03:22 PM
#4
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
-
Jan 6th, 2021, 03:36 PM
#5
Thread Starter
Addicted Member
Re: Vb6 elapsed time in days hours minutes
Originally Posted by westconn1
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
-
Jan 6th, 2021, 04:17 PM
#6
Re: Vb6 elapsed time in days hours minutes
-
Jan 6th, 2021, 04:30 PM
#7
Thread Starter
Addicted Member
Re: Vb6 elapsed time in days hours minutes
Originally Posted by baka
i dont need to compare two dates
i need count up timer, that counts keeps counting from the moment i press command1
-
Jan 6th, 2021, 04:41 PM
#8
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.
-
Jan 6th, 2021, 04:42 PM
#9
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.
-
Jan 6th, 2021, 04:42 PM
#10
Thread Starter
Addicted Member
Re: Vb6 elapsed time in days hours minutes
Originally Posted by baka
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
-
Jan 6th, 2021, 04:44 PM
#11
Re: Vb6 elapsed time in days hours minutes
We try to help you understand.
We don’t work for you.
-
Jan 6th, 2021, 04:48 PM
#12
Thread Starter
Addicted Member
Re: Vb6 elapsed time in days hours minutes
Originally Posted by Arnoutdv
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
-
Jan 6th, 2021, 04:53 PM
#13
Thread Starter
Addicted Member
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 = ...
-
Jan 6th, 2021, 05:07 PM
#14
Re: Vb6 elapsed time in days hours minutes
-
Jan 6th, 2021, 05:07 PM
#15
Re: Vb6 elapsed time in days hours minutes
-
Jan 6th, 2021, 05:42 PM
#16
Thread Starter
Addicted Member
Re: Vb6 elapsed time in days hours minutes
Originally Posted by jdc2000
it does not show how many days
-
Jan 6th, 2021, 06:50 PM
#17
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).
-
Jan 6th, 2021, 06:57 PM
#18
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).
-
Jan 6th, 2021, 07:25 PM
#19
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.
-
Jan 6th, 2021, 07:55 PM
#20
Re: Vb6 elapsed time in days hours minutes
Originally Posted by geekmaro
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
-
Jan 7th, 2021, 01:57 AM
#21
Thread Starter
Addicted Member
Re: Vb6 elapsed time in days hours minutes
Originally Posted by Schmidt
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
-
Jan 7th, 2021, 05:30 AM
#22
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
-
Jan 7th, 2021, 07:54 AM
#23
Re: [RESOLVED] Vb6 elapsed time in days hours minutes
Originally Posted by westconn1
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
-
Jan 7th, 2021, 10:14 AM
#24
Re: [RESOLVED] Vb6 elapsed time in days hours minutes
Originally Posted by Schmidt
Label1.Caption = Int(CDbl(t)) & " days " & Format$(t, "hh:mm")
I think just Int(t) (without the CDbl conversion) should also work (not tested).
-
Jan 8th, 2021, 09:42 PM
#25
Re: [RESOLVED] Vb6 elapsed time in days hours minutes
Originally Posted by Eduardo-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|