DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: I Help With Addign Time

  1. #1
    Join Date
    Jul 2005
    Posts
    203

    Question I Help With Addign Time

    When i add 1:00 and 2:00 it comes out as 1:002:00

    Plz Help

  2. #2
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    take a look at the DataAdd function, and see if that helps. This add two hours to one AM:
    DateAdd("h", 2, "1:00")

    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  3. #3
    Join Date
    Jul 2005
    Posts
    203
    thanks but it dosen't work when you are using 24 hour time

  4. #4
    Join Date
    Dec 2003
    Posts
    2,750
    It shouldn't matter. You can format the date however you wish.

    Maybe you could provide a code example of what you are doing?
    Paul
    ~~~~
    Microsoft MVP (Visual Basic)

  5. #5
    Join Date
    Jul 2005
    Posts
    203
    Text32.text = text25.text + Text30.text

    Text25 and text30.text = somthing in 24 hour time

  6. #6
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    I don't understand what you're trying to accomplish. If Time1 = 12:00 and Time2 = 18:00, what result do you expect by adding them together? 30:00?
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  7. #7
    Join Date
    Jul 2005
    Posts
    203
    you got it that is what i whant

  8. #8
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Try this:
    Code:
    Time1 = "12:45"
    Time2 = "18:50"
    Debug.Print AddTimes(Time1, Time2)
    
    
    Public Function AddTimes(ByVal t1 As String, ByVal t2 As String) As String
    
        Dim Time1() As String
        Dim Time2() As String
        Dim Hours As Integer
        Dim Minutes As Integer
    
        ' Split time strings on colon
        ' Time1(0) contains hour, Time1(1) contains minutes
        Time1 = Split(t1, ":")
        Time2 = Split(t2, ":")
        
        ' Add minutes
        Minutes = Val(Time1(1)) + Val(Time2(1))
        ' Add hours, plus minutes divided by 60
        Hours = Val(Time1(0)) + Val(Time2(0)) + (Minutes \ 60)
        ' Minutes = Remainder of Minutes \ 60
        Minutes = Minutes Mod 60
    
        ' Format as hh:mm and return
        AddTimes = CStr(Hours) & ":" & Format(Minutes, "0#")
    
    End Function
    P.S. -- Please do not start new threads with the same question. If we can answer your question, we'll do it in the original thread. Thank you!
    Last edited by Phil Weber; 07-11-2005 at 09:13 PM.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  9. #9
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    Quote Originally Posted by Code_Writer
    Text32.text = text25.text + Text30.text

    Text25 and text30.text = somthing in 24 hour time
    what you are doing here is adding (concatenating) two strings, this is why
    "12:00" + "18:00" = "12:0018:00"

    BTW what kind of time is "30:00" (that is the results you want)? It does not make any sense at all.
    BTW2 in fact, adding two dates does not make any sense. You can add a time interval to a date (this is why I suggested to use the DateAdd method)

    Can you please tell us exactly what you are trying to accomplish?
    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  10. #10
    Join Date
    Jul 2005
    Posts
    203
    Phil Weber Is there a simplier way to do it?

  11. #11
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    None that I know of. As Marco points out, what you're trying to do is an unusual application of time values. Typically, you would add a number of minutes or hours to a time to arrive at a new time, or subtract one time from another to determine the elapsed time. The only way I know how to do what you're asking is to convert the time strings into hour and minute values and manually add them.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  12. #12
    Join Date
    Jul 2005
    Posts
    203
    thanks I'll give that a try.

  13. #13
    Join Date
    Jul 2005
    Posts
    203
    i Need a simple why that i can do inside a privite sub

  14. #14
    Join Date
    Jul 2005
    Posts
    203
    never mind that code worked thanks

  15. #15
    Join Date
    Jul 2005
    Posts
    203

    Post I found a simpler way to do it

    Here is the easy way!!!!

    Code:
    Time1 = 14:23
    Time2 = 5:00
    
    H1 = Hour(Time1) + Hour(Time2)
    M1 = Minute(Time1) + Minute(Time2)
    
    If M1 < 60 Then GoTo DontDo
    Do
    M1 = M1 - 60
    H1 = H1 + 1
    Loop Until M1 < 60
    
    DontDo:
    
    If M1 = "0" Then M1 = "00"
    Answer = H1 & ":" & M1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center