Question
· Jul 27

How to Calculate Time Differences in Hours Between Two Timestamps in ObjectScript?

Hi all,

I’m working on a data transformation in IRIS where I need to calculate the difference in hours between two timestamps stored as %TimeStamp.

Example:

Set startTime = "2024-07-12 08:30:00"
Set endTime = "2024-07-12 15:15:00"

I’d like to calculate the number of hours (with decimals, like 6.75) between them. What's the recommended way to do this in ObjectScript? I’ve seen examples using $ZDATETIME, but I’m not sure if that’s the cleanest approach.

Any suggestions or best practices would be appreciated!

Thanks,
Jhonn Marie

Product version: Adaptive Analytics 2023.2
Discussion (3)3
Log in or sign up to continue

To calculate the difference in hours (with decimals) between two %TimeStamp values in ObjectScript, you can use the $ZDATETIMEH function to convert the timestamps into $HOROLOG format, and then calculate the difference using appropriate arithmetic. Here is an example approach:

Code Implementation

ClassMethod CalculateHourDifference(startTime As %TimeStamp, endTime As %TimeStamp) As %Float
{
    // Convert start and end times to $HOROLOG format
    Set startTimeH = $ZDATETIMEH(startTime, 3)
    Set endTimeH = $ZDATETIMEH(endTime, 3)

    // Calculate the difference in days and seconds
    Set dayDiff = $PIECE(endTimeH, ",", 1) - $PIECE(startTimeH, ",", 1)
    Set timeDiff = $PIECE(endTimeH, ",", 2) - $PIECE(startTimeH, ",", 2)

    // Combine the difference into hours
    Set totalHours = dayDiff * 24 + (timeDiff / 3600)

    Return totalHours
}

Explanation

  1. $ZDATETIMEH(timestamp, precision) converts a %TimeStamp into $HOROLOG format with the precision value 3 for best compatibility [1][2].
  2. $HOROLOG format consists of two parts: the number of days since 31 Dec 1840 and the number of seconds since midnight of the current day.
  3. By calculating day and second differences separately, you can then convert the total time difference into hours.

Example

Using your provided timestamps:

Set startTime = "2024-07-12 08:30:00"
Set endTime = "2024-07-12 15:15:00"
Write ##class(YourClass).CalculateHourDifference(startTime, endTime)
// Output: 6.75

This approach ensures precision and clarity while working with timestamps in ObjectScript. [1][2]

Sources:

You can use the SQL DATEDIFF function in ObjectScript to get the number of minutes and divide by 60:

write $system.SQL.Functions.DATEDIFF("mi","2024-07-12 08:30:00","2024-07-12 15:15:00")/60  
6.75

Documentation is here: https://docs.intersystems.com/iris20252/csp/documatic/%25CSP.Documatic.c...

You can pass "hh" in as the first argument to get hours, but only returns whole hours rounded up. If you need second accuracy, you can pass in "ss" for "seconds" and divide by 3600 instead.

Dear @Toni Crooz ,

you can use this function :

Set totalMinutes = $SYSTEM.SQL.DATEDIFF("mi", startTime, endTime)

But using this you have to convert them back into years,days and hours

Another way you can check out the below query :This gives you no of days

SELECT DATEDIFF(day, TO_DATE('2025-07-20', 'YYYY-MM-DD'), TO_DATE('2025-07-28', 'YYYY-MM-DD')) AS Diff