Solved

Add seconds to Time

Posted on 2001-06-10
5
188 Views
Last Modified: 2010-05-02
I have a Time in a textbox as hhmmss.  I need to add some seconds to the time and have the result as hhmmss.

The following code almost works.  Can someone help me with the almost part.  <Grin>

I need a lot better solution than where I'm heading with this. The math seems to work, but the output format does not keep the hhmmss format.  It suppresses leading zeros, or if it should be 000045, my output shows 45.
Times are never more than 18 hours apart, so the general code flow is OK.


Private Sub Command1_Click()
   
    txtDifference.Text = CStr(AddSecToTime(txtStart.Text, txtStop.Text))
   
End Sub

Private Function AddSecToTime(Time1 As String, Seconds As Long) As Long

    Dim tm1 As Long
    Dim TimeInSec As Long
    Dim Hours As Long
    Dim Minutes As Long
    Dim TimeInSec As Long
   
    tm1 = 3600 * CLng(Mid(Time1, 1, 2)) + 60 * CLng(Mid(Time1, 3, 2)) _
       + CLng(Mid(Time1, 5, 2))
       
    TimeInSec = tm1 + Seconds
   
    If TimeInSec > 86400 Then
        TimeInSec = TimeInSec - 86400
    End If
   
    Hours = Int(TimeInSec / 3600)
    TimeInSec = TimeInSec Mod 3600
   
    Minutes = Int(TimeInSec / 60)
    TimeInSec = TimeInSec Mod 60
   
    AddSecToTime = Hours & Minutes & TimeInSec

End Function
0
Comment
Question by:MitchSavage
  • 3
5 Comments
 
LVL 5

Expert Comment

by:ianouii
ID: 6175551
u can't have leading zero bcoz your datatype is long instead of string. in this case,

just amend something in your code, hope it helps.

Private Function AddSecToTime(Time1 As String, Seconds As Long) As string

....
....  

AddSecToTime = cstr(Hours) & cstr(Minutes) & cstr(TimeInSec)

good luck.


0
 
LVL 5

Accepted Solution

by:
ianouii earned 100 total points
ID: 6175564
then use a function called format.

format(cstr((TimeInSec, "0000000"))

good luck.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6175575
Hi, for the time value in txtStart & txtStop, i suggest you and a ":" between those value, look nicer, then you can use Split function to get the value of time in an array.

Alternatively, you can use TimeValue, TimeSerial function to do it, i think its much easier.
0
 

Author Comment

by:MitchSavage
ID: 6175619
ianouii,

Thanks, it works great now,  You got the almost out of my "It almost works".

Both of your suggestions were required to make it work.  .... As String.....    and Format(cstr........

Thanks again and
Best Regards,

Mitch
0
 
LVL 5

Expert Comment

by:ianouii
ID: 6175634
u're welcomed & thanks for your good grade. happy coding then. cheers!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Saving history changes to sub form 4 44
Put text in a picture ASP.NET C# 2 58
Copy a row 12 62
VBA - If Bookmark = "XXBOOKMARKXX" then 15 29
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question