Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2005

    Unanswered: VB Code to compute the time difference

    Greetings to all,

    Is there a simple code that would compute for the time difference? Example from 10PM to 3AM. The result should be 5 hours. I have used DateDiff [Ex.DateDIff("h",10PM,3AM)], but the result is 19 hours...wherein it computes the time difference between 3AM to 10PM. I tried changing the time variants [Ex.DateDIff("h",3AM,10PM)], but the result is -19 hours.

    Thanks in advance.

  2. #2
    Join Date
    Jun 2004
    Arizona, USA
    This is because dates (used in the datediff function) contain both the date and the time.

    If you do not include the date portion (only the time portion) then it is assumed to be day zero (12:00 AM, 30 Dec 1899)

    In any event, the date difference between 3AM and 10PM of the same day IS 19 hours.

    If you need to determine the time difference between a time today, and a time on the following day, you need to include the day as well as the time in values that you pass to the DateDiff function.

    One way would be to convert the second time to a double, add 1 to the converted value, then convert it back to a date. Ex:

    Debug.Print DateDiff("h",10PM,cDate(cDbl(3AM) + 1))
    Remember that dates in VB are really doubles cast as dates. 1.5 corresponds to 1 and 1/2 days after date 0, or 12 Noon on 31 Dec 1899.
    Last edited by loquin; 09-26-06 at 17:52.
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Posting Permissions

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