Results 1 to 11 of 11

Thread: Adding Time to Time- is it possible ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    156

    Cool

    hi there ,

    i have a table that include the next fields :
    1. EventID
    2. StartTime
    3. EndTime

    i want to sum (EndTime-StartTime) but there is a problem !!!
    4 eg. if i m adding 12:00 to 11:00 i get 23:00 , so far so good ! BUT if i m adding 12:00 to 13:00 i get 1:00 Not so good !!!

    my question is how to add time and recive the correct sum value ?

    tnx lirlir
    The MORE I get to know,
    I realize that I know NOTHING !

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    This is a bit crude but it works:
    Code:
    Dim MyH, MyM As Integer
    Dim MyTime, MyTime2 As String
    MyTime = Format("1:23", "hh:mm")
    MyTime2 = Format("3:41", "hh:mm")
    MyH = Val(Left$(MyTime, 2)) + Val(Left$(MyTime2, 2))
    MyM = Val(Right$(MyTime, 2)) + Val(Right$(MyTime2, 2))
    If MyH > 12 Then MyH = MyH - 12
    If MyM > 60 Then MyH = MyH + 1: MyM = MyM - 60
    MsgBox = Format(MyH & ":" & MyM, "hh:mm")
    Hope it works for you,

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    156
    not so good

    i tried that code

    Private Sub Form_Activate()
    Dim MyH, MyM As Integer
    Dim MyTime, MyTime2 As String

    MyTime = Format("23:23", "hh:mm")
    MyTime2 = Format("3:41", "hh:mm")

    MyH = Val(Left$(MyTime, 2)) + Val(Left$(MyTime2, 2))
    MyM = Val(Right$(MyTime, 2)) + Val(Right$(MyTime2, 2))
    If MyH > 12 Then MyH = MyH - 12
    If MyM > 60 Then MyH = MyH + 1: MyM = MyM - 60
    Call MsgBox(Format(MyH & ":" & MyM, "hh:mm"))

    End Sub


    and got this result : 15:04
    The MORE I get to know,
    I realize that I know NOTHING !

  4. #4
    Guest
    Declare it as Date.

    Code:
    Function AddDate(Time1 As Date, Time2 As Date) As Date
        AddDate = CDate(Time1 + Time2)
    End Function
    
    Private Sub Command1_Click()
        Print AddDate("1:59", "1:02")
    End Sub

  5. #5
    Member
    Join Date
    May 2000
    Location
    Canada
    Posts
    52
    check out the dateadd function

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    156
    What r u talking about d.paulson, what function.
    pls write the syntax if u can.

    tnx lirlir
    The MORE I get to know,
    I realize that I know NOTHING !

  7. #7
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Code:
    Public Function DisplayTimeSum(Time1 as Time, Time2 as Time) as String
      Dim sglResult as Single
    
      sglResult = CSng(Time1 + Time2) * 24
    
      DisplayTimeSum = format(Int(sglResult),"00:") & format((sglResult - int(sglResult)) * 60,"00")
    End Function
    
    Msgbox DisplayTimeSume(TimeValue("23:23"),TimeValue("3:41"))
    You will find the answer is 27:04

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    156
    hey tnx,

    it's look fulfilling ,but its raise an error as if the
    "time" if User-defined type is undefined.

    what is the reference to the "time".

    tnx lirlir
    The MORE I get to know,
    I realize that I know NOTHING !

  9. #9
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Ah sorry... replace "Time" with "Date" as a datatype and it should work perfectly.

    You might want to add some checking or change the datatypes ot strings and then perform a "TimeValue" on them but the choice is yours.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    156

    Thumbs up

    Yes Sir,
    u r the B-52 of all programmers.

    very helpfull indeed !!!

    tnx mate

    lirlir
    The MORE I get to know,
    I realize that I know NOTHING !

  11. #11
    Member
    Join Date
    May 2000
    Location
    Canada
    Posts
    52
    Returns a Variant (Date) containing a date to which a specified time interval has been added.

    Syntax

    DateAdd(interval, number, date)

    The DateAdd function syntax has thesenamed arguments:

    Part Description
    interval Required.String expression that is the interval of time you want to add.
    number Required.Numeric expression that is the number of intervals you want to add. It can be positive (to get dates in the future) or negative (to get dates in the past).
    date Required. Variant (Date) or literal representing date to which the interval is added.


    Settings

    The intervalargument has these settings:

    Setting Description
    yyyy Year
    q Quarter
    m Month
    y Day of year
    d Day
    w Weekday
    ww Week
    h Hour
    n Minute
    s Second


    Remarks

    You can use the DateAdd function to add or subtract a specified time interval from a date. For example, you can use DateAdd to calculate a date 30 days from today or a time 45 minutes from now.

    To add days to date, you can use Day of Year ("y"), Day ("d"), or Weekday ("w").

    The DateAdd function won't return an invalid date. The following example adds one month to January 31:

    DateAdd("m", 1, "31-Jan-95")

    In this case, DateAdd returns 28-Feb-95, not 31-Feb-95. If date is 31-Jan-96, it returns 29-Feb-96 because 1996 is a leap year.

    If the calculated date would precede the year 100 (that is, you subtract more years than are in date), an error occurs.

    If number isn't aLong value, it is rounded to the nearest whole number before being evaluated.

    Note The format of the return value for DateAdd is determined by Control Panel settings, not by the format that is passed in date argument.

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