 # Thread: Subtracting 2 Time Values

1. ## How can one subtract two times (e.g., one the current time, and one preset time) so as to find the difference between those times in terms of hours/minutes/seconds...

I am new to programming...excuse the idiocy of this question, as I am sure the result is extremely simple.

Thanks!  Reply With Quote

2. Here's a quick sample
Code:
`TimeSerial(0, 0, DateDiff("s", time, "4:54"))`  Reply With Quote

3. I am sorry, but I don't see how to make this work...:-(

Ugh...too much for me!  Reply With Quote

4. Well probably my fault then, I should have explained it better. Time and date in vb are presented as Date data type, which can be assigned by converting strings to date, and also retrieved as a string. The date expression i showed you is the result from using TimeSerial function and DateDiff function, which of the later will return the difference in seconds (by it's first parameter) of two dates, on which i have assigned time, as current time and "4:54" that i picked as your "preset time" you could substitute this with your date expression or variable. TimeSerial will return a date format by calculating the time from 0:00:00 + amount of seconds DateDiff function is returning.

You could assign the whole expression to a date datatype but also a string if you desire.  Reply With Quote

5. ## Thanks

Thanks for the help! I will try to figure it out...it is just hard to see without the actual coding (you know, which variables to declare, where to put them, etc.). I am getting the hang of this, though, albeit slowly. Anyway, thanks for the help.  Reply With Quote

6. If you want variables, here's another try:
Code:
```Dim preset as date,result as date
preset=CDate("4:54")
result = TimeSerial(0, 0, DateDiff("s", time, preset))```  Reply With Quote

7. I used the code but it returned the following value:

1. With the variable "s" it gave an overflow error

2. I changed it to "h" and the result was
12:00:17 AM

What am I doing wrong?

Thanks!  Reply With Quote

8. The example below shows u how to get the differential hour difference between two times.

Dim d As Date
Dim d1 As Date
Dim d2 As Long
d = "2:00 PM"
d1 = "7:00 PM"
d2 = DateDiff("h", d, d1)
MsgBox "Difference Between two dates:" & " " & d2

if u want differential minutes difference between two times, u can modify the DateDiff("n",d,d1).
if u want differential seconds difference between two times, u can modify the DateDiff("s",d,d1).

Okay mate, I think this may resolve ur problem.  Reply With Quote

9. overflow is due to Datatype Long which Datediff returns won't hold larger integer values than 2^31-1 (about 2 billion) neither smaller than -2^31. I suppose you entered a date expression containing the date part that caused it go over the limit, since more than 68 years is over 2 billion seconds.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured