# Thread: Tech Tip: Elapsed Time Function

1. Guest.Visitor Guest

## Tech Tip: Elapsed Time Function

I am little confused. Why would I want to write an entire procedure to do what is already available (and BETTER!) in RPG? You are using Date and Time fields. Why not use a Timestamp field? This would allow to calculate elpased times less than a second (of course how accurate would these times really be??). You are using the %Diff() BIF. Why not use the %Diff() against the 2 Timestamp fields directly? You are using you own code to return the elapsed time in different units (Hours, Days, etc.). Why not use the optional 3rd parm on %Diff()? As you can see, ALL of the functionallity you have written into your procedure are directly available by using 2 Timestamp fields (with the TIME op-code) and the %Diff() BIF. So, what's the point? Brian

2. Guest.Visitor Guest

## Tech Tip: Elapsed Time Function

If you've already got the "From" and "To", why not just use the SUBDUR opcode, then you'll get the difference with one line of code. Matt

3. Guest.Visitor Guest

## Tech Tip: Elapsed Time Function

What you're saying is true; however, that could be said of any user-defined function. The point of this is to take what would otherwise be a multi-step process and convert it into a format that can be used on a single EVAL statement. While this can be accomplished with SUBDUR, the function will do both date and time in one step. The tech tip itself was written in response to a question that I've heard other iSeries programmers ask. Mike

4. Guest.Visitor Guest

## Tech Tip: Elapsed Time Function

Another way to easily compute duration given two dates and times is using builtin function %DIFF() as follows: `dur=%diff(d1+t1:d2+t2:*minutes);` That is, you can "add" together a date value and a time value to get a timestamp value, and you can then compute the difference of the two timestamps. Cheers! Hans

5. Guest.Visitor Guest

## Tech Tip: Elapsed Time Function

Hello, why using a couple of statements, when SQL needs only one. Here an example how to calulate date/time difference with embedded SQL: Birgitta
Code

6. Senior Member
Join Date
Dec 2007
Posts
997

## Tech Tip: Elapsed Time Function

This is a discussion about Tech Tip: Elapsed Time Function.