dcsimg
Results 1 to 9 of 9

Thread: Subtracting 2 Time Values

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    6

    Unhappy

    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!

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Here's a quick sample
    Code:
    TimeSerial(0, 0, DateDiff("s", time, "4:54"))
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    6
    I am sorry, but I don't see how to make this work...:-(

    Ugh...too much for me!

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    6

    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.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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))
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    6
    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!

  8. #8
    Guest
    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.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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


Click Here to Expand Forum to Full Width