[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# TIME DIFFERENCE IN SECONDS

Posted on 2006-06-01
Medium Priority
227 Views
Hi guys,

I have a field on a DB2 database that has the date and time a field was created in the following format:

YYYYMMDDhhmmss

for example, for may 9th 2006 at 2:43:23 PM the field has something like

20060509144323 (note 2 pm is changed by 14, to european format).

I need to know for each field the time difference in seconds comparing with the current date and time - now() -

How can I do that?!

TKS
0
Question by:pvg1975
• 2
• 2
• 2
• +1

Expert Comment

ID: 16810441
Did you try the datediff funx?

datediff("s",t1,t2)

For ex:
datediff("s","1:59:03 PM","2:00:00 PM")
0

Author Comment

ID: 16810459
Yes, but the format of the date is not a date/time format value:

20060509144323
0

Accepted Solution

coderblues earned 1000 total points
ID: 16810610
Then something like this to convert it:

y = "20060509144323"

vyear = Left(y, 4)
vmonth = Mid(y, 5, 2)
vday = Mid(y, 7, 2)
vhour = Mid(y, 9, 2)
vmin = Mid(y, 11, 2)
vsec = Right(y, 2)

actdate = CDate(vmonth + "/" + vday + "/" + vyear)
actdate = actdate + CDate(" " + vhour + ":" + vmin + ":" + vsec)
0

LVL 10

Assisted Solution

fostejo earned 1000 total points
ID: 16810624
pvg1975,

Try parsing/converting what you get from the DB2 database into a suitable format to convert to a 'proper' date with CDATE(), prior to using the method sugessted by coderblues.

I haven't tested but something like this rough example perhaps:

sODate="20060509144323"    ' Just an example, should be the actual data grabbed from the database..

sYear=left\$(sODate,4)
sMonth=mid\$(sODate,5,2)
sDay=mid\$(sODate,7,2)

sHour=mid\$(sODate,9,2)
sMin=mid\$(sODate,11,2)
sSec=right\$(sODate,2)

sNewDate=sMonth&"/"&sDay&"/"&sYear&" "&sHour&":"&sMin&":"&sSec   ' gives something like "05/09/2006 14:43:23" which CDate should understand.

lSecDiff=DateDiff("s",CDate(sNewDate),now())

cheers,
0

LVL 10

Expert Comment

ID: 16810642
Great minds coderblues!
0

LVL 5

Expert Comment

ID: 16810882
I am not sure this will help but here is a function that I use.

Function ElapsedTime(tStart As Variant, tStop As Variant) As String
' **************************************************************
'Notes:         : Times passed to this function should be
'               : in valid format (e.g., hh:mm.ss).  Otherwise,
'               : function will return 0:00:00
'    ElapsedTime(Format(str, "hh:mm:ss"), Format(Now, "hh:mm:ss"))
' ****************************************************************

On Error GoTo END_ELAPSEDTIME

Dim dtr, dtl, jml As Long

dtl = (Hour(tStart) * 3600) + (Minute(tStart) * 60) + (Second(tStart))

dtr = (Hour(tStop) * 3600) + (Minute(tStop) * 60) + (Second(tStop))

If tStop < tStart Then
jml = 86400
Else
jml = 0
End If
jml = jml + (dtr - dtl)

ElapsedTime = Format(str(Int((Int((jml / 3600)) Mod 24))), "00") + ":" + Format(str(Int((Int((jml / 60)) Mod 60))), "00") + ":" + Format(str(Int((jml Mod 60))), "00")

END_ELAPSEDTIME:

End Function
0

LVL 5

Expert Comment

ID: 16810890
Oh yeah, you can change the format to match yours or just change yours to what the function use.
0

Author Comment

ID: 16811960
Thanks guys!
0

## Featured Post

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
###### Suggested Courses
Course of the Month18 days, 17 hours left to enroll