|
-
Feb 28th, 2005, 03:02 PM
#1
Thread Starter
Member
calculate time
hi
how can i making plus operation between two or three times like this :
00:00:25 + 00:00:15 + 00:01:00 = 00:01:40
please help
-
Feb 28th, 2005, 03:50 PM
#2
Re: calculate time
You can use this:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim mytime
mytime = TimeValue("00:00:25") + _
TimeValue("00:00:15") + TimeValue("00:01:00")
Debug.Print Format(mytime, "h:m:s")
End Sub
-
Feb 28th, 2005, 03:57 PM
#3
Re: calculate time
just another way if you already had them as dates. convert to a double then back to date
VB Code:
Dim d1 As Date, d2 As Date, d3 As Date
d1 = CDate("00:00:25")
d2 = CDate("00:00:15")
d3 = CDate("00:01:00")
MsgBox CDate(CDbl(d1) + CDbl(d2) + CDbl(d3))
casey.
-
Feb 28th, 2005, 03:59 PM
#4
Re: calculate time
 Originally Posted by vbasicgirl
just another way if you already had them as dates. convert to a double then back to date
VB Code:
Dim d1 As Date, d2 As Date, d3 As Date
d1 = CDate("00:00:25")
d2 = CDate("00:00:15")
d3 = CDate("00:01:00")
MsgBox CDate(CDbl(d1) + CDbl(d2) + CDbl(d3))
casey.
Why DOUBLE and not INTEGER, or LONGWORD?
DOUBLE is such an inaccurate datatype - I would be afraid to use it.
-
Feb 28th, 2005, 04:04 PM
#5
Re: calculate time
 Originally Posted by vbasicgirl
just another way if you already had them as dates. convert to a double then back to date
VB Code:
Dim d1 As Date, d2 As Date, d3 As Date
casey.
You probably could skip the conversion (per se) and do:
VB Code:
Option Explicit
Private Sub Form_Load()
MsgBox CDate("00:00:25") + CDate("00:00:15") + CDate("00:01:00")
End Sub
Bruce.
Last edited by Bruce Fox; Feb 28th, 2005 at 04:08 PM.
-
Feb 28th, 2005, 04:14 PM
#6
Re: calculate time
David,
I took the liberty of optimising your example:
VB Code:
Dim mytime As Date
mytime = CDate(TimeValue("00:00:25") + TimeValue("00:00:15") + TimeValue("00:01:00"))
Debug.Print Format(mytime, "hh:mm:ss")
Bruce.
-
Feb 28th, 2005, 04:22 PM
#7
Re: calculate time
Bruce Fox - yes, you are right, thanks for that.
szlamany - i was always told that time is actually a double, where a day is 1 and time is a fraction of that (double).
have i been told wrong ?
thank you.
casey.
-
Feb 28th, 2005, 04:28 PM
#8
Re: calculate time
 Originally Posted by vbasicgirl
szlamany - i was always told that time is actually a double, where a day is 1 and time is a fraction of that (double).
have i been told wrong ?
thank you.
casey.
Where have you seen that reference?
Whenever you convert anything to floating point, you end up with precision problems - since it's using logarithms to do the math and storing the values in an "exponential/mantisa" format.
I've seen all these posts about how to do this, but if I had my choice I would probably take the hours*60 to turn them into minutes, minutes and multiply by 60 to turn them into seconds - add all that up so that I'm dealing "seconds" only.
Then add all the values (seconds that is) together - and then format that "total number of seconds" back into a hr:mn:ss value.
-
Feb 28th, 2005, 04:39 PM
#9
Re: calculate time
From MSDN
Date data type
A data type used to store dates and times as a real number. Date variables are stored as 64-bit (8-byte) numbers. The value to the left of the decimal represents a date, and the value to the right of the decimal represents a time.
http://msdn.microsoft.com/library/de...tedatatype.asp
Bruce.
Last edited by Bruce Fox; Feb 28th, 2005 at 04:43 PM.
-
Feb 28th, 2005, 04:42 PM
#10
Re: calculate time
DATES are stored as DOUBLE - that's what that says.
It also says this:
When other numeric types are converted to Date, values to the left of the decimal represent date information while values to the right of the decimal represent time. Midnight is 0 and midday is 0.5. Negative whole numbers represent dates before 30 December 1899.
That's for when a numeric type if converted to a DATE - that in no way indicates how the time is stored in the DOUBLE of the DATE FORMAT datatype!
-
Feb 28th, 2005, 04:45 PM
#11
Re: calculate time
i just found this in my notes, i can see where i got it from. thanks for the info.
A common question is what is the date or time right now? To answer that, VB has 3 functions available. The Date function returns the current date, the Time function returns the current time and the Now function returns the current time and date.
Actually, all three return time/date because all three return a variable of type Date. In previous versions of VB, there was no Date type. Instead, the functions returned a Double. Today, you can still treat the Date Type as if it were a double.
The Date type is an 8 byte double precision floating point number representing the number of days since midnight, Dec 30, 1899. The fractional part of the date represents the time in days. So 12 noon would be half a day, or 0.5.
For instance if the time/date now were Aug 8, 2002 6:00 PM, the values of the three functions would be:
Now() = 37476.75
Date() = 37476
Time() = 0.75
thank you.
casey.
-
Feb 28th, 2005, 05:06 PM
#12
Re: calculate time
 Originally Posted by Bruce Fox
David,
I took the liberty of optimising your example:
VB Code:
Dim mytime As Date
mytime = CDate(TimeValue("00:00:25") + TimeValue("00:00:15") + TimeValue("00:01:00"))
Debug.Print Format(mytime, "hh:mm:ss")
Bruce.
Thanks. I usually go back and optimize thinks when i have them working, but had to go out, so I posted with the *ugly* variant type. i was actually going to try to see how date worked, because I knew there was no TIME type
-
Feb 28th, 2005, 05:50 PM
#13
Thread Starter
Member
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
|