If I have time like this
02:01:50
hours:minutes:seconds
and I have another time like this
00:00:10
and i want to add times to each other to get this
02:02:00
how to do this ?
i want to add time to another time to get a result
Printable View
If I have time like this
02:01:50
hours:minutes:seconds
and I have another time like this
00:00:10
and i want to add times to each other to get this
02:02:00
how to do this ?
i want to add time to another time to get a result
i tried this
Dim x As Date
Dim y As Date
x = "23:58:23"
y = "00:01:26"
z = x + y
MsgBox z
result was 23:59:49
but if i try this
Dim x As Date
Dim y As Date
x = "23:58:23"
y = "00:06:26"
z = x + y
MsgBox z
it says
31/12/1899 00:04:49
instead of 01:00:04:49 days:hours:minutes:seconds
any idea ?
Use the dateadd function.
how ?
You have to take into consideration spanning of days. Think of it as the carry process in arithmetic, if time was ones place and days was tenths place and you added 6+6 you'd get 12 (1 day in tenths place, 2 time in ones place). But during interpretation you disregard days giving you just 2.
Same goes for determining duration... what if you try to subtract an hour from 00:05 (or 12:05 AM).
Consider the date/day in your calculations when spanning days. Unless you address that, whatever method of calculation you do (+ operator between datetime variables, dateadd, datediff) answer would still be wrong.
is there any function to read time this format
00:00:00:00:00:00
Years:Months:Days:Hours:Minutes:Seconds
Like this example: 01:07:12:06:55:24
1 year and 7 months and 12 days bla bla bla
and I can add to it another time like if i get time different like this:
00:23:27
I can add 00:23:27 to this 01:07:12:06:55:24
please help urgent
thanks
If you want to introduce your own format and you want interpretation to be regional settings independent then you will have to parse above format yourself with Split(). After parse to string array you assign values to DateSerial() and TimeSerial() parameters accordingly.
Then to check if the value in the created datetime variable from DateSerial() + TimeSerial() is correct you try recreate the string with Format() and compare if its the same as original input.
Try to avoid thinking in terms of string representation, as you will have to address techincal issues related to interpreting dates/time in strings. Use the datetime data type, it already contains info on year month day hour minute second. Coming up with your own representation is just reinventing the wheel and you'll have to shoulder everything that comes with it (coding, parsing, display, etc).
My standard approach has always been to convert to a base unit of seconds, and then after the calculations are done, format the resulting number of seconds as a time display. (eg: 666 seconds displays as 11:06)
Using this technique, you'd calculate your two amounts in seconds:
(2 * 3600) + (1 * 60) + 50 = 7310
(0 * 3600) + (0 * 60) + 10 = 10
Add them together, then send the resulting value to the formatting function. Here's the function I've been using forever:Using this function in the immediate window, we'll get:Code:' Returns "0:12", "14:26", "6:00:12", etc...
Public Function SecondsToTime(ByVal plngSeconds As Long) As String
Dim strReturn As String
If plngSeconds >= 3600 Then
strReturn = plngSeconds \ 3600
plngSeconds = plngSeconds Mod 3600
strReturn = strReturn & ":" & Format(plngSeconds \ 60, "00") & ":" & Format(plngSeconds Mod 60, "00")
ElseIf plngSeconds > 59 Then
strReturn = plngSeconds \ 60 & ":" & Format(plngSeconds Mod 60, "00")
Else
strReturn = "0:" & Format(plngSeconds, "00")
End If
SecondsToTime = strReturn
End Function
?SecondsToTime(7320)
2:02:00
Note that the DateDiff() function is very handy for coming up with the seconds between two datetimes. As for your followup question, store the original value -- which includes year, month, day, hours, minutes and seconds -- as a datetime (date) value. Convert the "time to add" value to a number of seconds, and then use DateAdd() to add that many seconds to the datetime.
If he wants to display in hh:mm:ss format with logically correct info then he will have to display the proper duration for hours. If there's day spanning then hh should be hh from time portion of datetime plus 24 * day portion for him to be able to handle 26:00:00 or 1 day 2 hours duration. Or the hh portion would have to be calculated and displayed separately (concatenated) since mm:ss portions can be handled by Format().
That I believe is the issue, changing method (eg dateadd, datediff, arithmetic on datetime variables, etc) from one way to another would not address that iuf you already have the correct duration in datetime (but incorrect info displayed).
I think you're right. I originally thought he wanted to add two stopwatch-style values together. As in, 12 hours plus 13 hours = 25 hours, not 1 am/pm.