Page 1 of 1

How to find the difference between two timestamps

Posted: Wed Nov 19, 2003 11:33 pm
by girishoak
Hi,

I have two timestamps in the format yyyy-mm-dd hh24:mi:ss. I want to find the exact difference (sec level). Is there any in-buit routine that can I give me the difference. I know how to get that using Basics.

Thanks,
Girish Oak

Posted: Thu Nov 20, 2003 12:04 am
by kcbland
This is the code from my own set of tools for KBATimestampDeltaSeconds, which takes two timestamps as arguments and returns to you the difference in seconds:

Code: Select all

      StartDate = LEFT(Arg1,10)
      StartTime = RIGHT(Arg1,8)
      FinishDate = Left(Arg2,10)
      FinishTime = RIGHT(Arg2,8)

      InternalStartDate = ICONV(StartDate,"D-YMD[4,2,2]")
      InternalStartTime = ICONV(StartTime,"MTS")
      InternalFinishDate = ICONV(FinishDate,"D-YMD[4,2,2]")
      InternalFinishTime = ICONV(FinishTime,"MTS")

      NetDays = InternalFinishDate - InternalStartDate

      NetTime = 0
      Begin Case
         Case InternalFinishTime >= InternalStartTime
            NetTime = InternalFinishTime - InternalStartTime
         Case InternalFinishTime < InternalStartTime
            NetTime = ((24 * 60 * 60) - InternalStartTime) + InternalFinishTime
            NetDays -= 1                 
      End Case

* Number of days difference * 24 hours * 60 minutes * 60 seconds + number of seconds elapsed

      TotalElapsedSeconds = (NetDays * 24 * 60 * 60) + NetTime

      If TotalElapsedSeconds < 0 or NetDays < 0 Then TotalElapsedSeconds = 0

      Ans = TotalElapsedSeconds

Posted: Thu Nov 20, 2003 12:06 am
by kcbland
Here's a version KBATimestampDeltaDays that gives you the answer rounded to days:

Code: Select all

      StartDate = LEFT(Arg1,10)
      StartTime = RIGHT(Arg1,8)
      FinishDate = Left(Arg2,10)
      FinishTime = RIGHT(Arg2,8)

      InternalStartDate = ICONV(StartDate,"D-YMD[4,2,2]")
      InternalStartTime = ICONV(StartTime,"MTS")
      InternalFinishDate = ICONV(FinishDate,"D-YMD[4,2,2]")
      InternalFinishTime = ICONV(FinishTime,"MTS")

      NetDays = InternalFinishDate - InternalStartDate

      NetTime = 0
      Begin Case
         Case InternalFinishTime >= InternalStartTime
            NetTime = InternalFinishTime - InternalStartTime
         Case InternalFinishTime < InternalStartTime
            NetTime = ((24 * 60 * 60) - InternalStartTime) + InternalFinishTime
            NetDays -= 1                 
      End Case

* Number of days difference * 24 hours * 60 minutes * 60 seconds + number of seconds elapsed

      TotalElapsedSeconds = (NetDays * 24 * 60 * 60) + NetTime

      If TotalElapsedSeconds < 0 or NetDays < 0 Then TotalElapsedSeconds = 0

      Ans = OCONV(TotalElapsedSeconds / (60 * 60 * 24), "MD0")

Posted: Thu Nov 20, 2003 12:15 am
by girishoak
Thanks for quick reply

Girish Oak

Posted: Mon Oct 20, 2008 9:34 am
by Nisusmage
Why not use these?

MeasureTime YearsToSeconds
MeasureTime WeeksToSeconds

Look in the help file under "Measurement Transforms - Time"

Posted: Mon Oct 20, 2008 9:46 am
by chulett
You sure they existed five years ago? :wink:

Posted: Mon Oct 20, 2008 10:28 pm
by Nisusmage
Haha.. point taken.

I never looked at the date.
But, hopefully it'll update the thread in case someone else come along.