Results 1 to 13 of 13

Thread: calculate time

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2003
    Posts
    55

    Unhappy 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

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: calculate time

    You can use this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim mytime
    5. mytime = TimeValue("00:00:25") + _
    6.        TimeValue("00:00:15") + TimeValue("00:01:00")
    7. Debug.Print Format(mytime, "h:m:s")
    8. End Sub

  3. #3
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: calculate time

    just another way if you already had them as dates. convert to a double then back to date
    VB Code:
    1. Dim d1 As Date, d2 As Date, d3 As Date
    2.  
    3. d1 = CDate("00:00:25")
    4. d2 = CDate("00:00:15")
    5. d3 = CDate("00:01:00")
    6.  
    7. MsgBox CDate(CDbl(d1) + CDbl(d2) + CDbl(d3))

    casey.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: calculate time

    Quote Originally Posted by vbasicgirl
    just another way if you already had them as dates. convert to a double then back to date
    VB Code:
    1. Dim d1 As Date, d2 As Date, d3 As Date
    2.  
    3. d1 = CDate("00:00:25")
    4. d2 = CDate("00:00:15")
    5. d3 = CDate("00:01:00")
    6.  
    7. 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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: calculate time

    Quote Originally Posted by vbasicgirl
    just another way if you already had them as dates. convert to a double then back to date
    VB Code:
    1. Dim d1 As Date, d2 As Date, d3 As Date
    casey.


    You probably could skip the conversion (per se) and do:
    VB Code:
    1. Option Explicit
    2. Private Sub Form_Load()
    3.  
    4.     MsgBox CDate("00:00:25") + CDate("00:00:15") + CDate("00:01:00")
    5.  
    6. End Sub


    Bruce.
    Last edited by Bruce Fox; Feb 28th, 2005 at 04:08 PM.

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: calculate time

    David,

    I took the liberty of optimising your example:
    VB Code:
    1. Dim mytime As Date
    2.  
    3.     mytime = CDate(TimeValue("00:00:25") + TimeValue("00:00:15") + TimeValue("00:01:00"))
    4.            
    5.     Debug.Print Format(mytime, "hh:mm:ss")




    Bruce.

  7. #7
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    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.

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: calculate time

    Quote 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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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.

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    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.

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: calculate time

    Quote Originally Posted by Bruce Fox
    David,

    I took the liberty of optimising your example:
    VB Code:
    1. Dim mytime As Date
    2.  
    3.     mytime = CDate(TimeValue("00:00:25") + TimeValue("00:00:15") + TimeValue("00:01:00"))
    4.            
    5.     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

  13. #13

    Thread Starter
    Member
    Join Date
    Aug 2003
    Posts
    55

    Re: calculate time

    thanx guys , it works great

    thanks again 2 all

Posting Permissions

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



Click Here to Expand Forum to Full Width