Duration Calculation

I have a simple parameter on a report called duration, and is calculated as follows:


I get the correct time difference + 2 hours, it’s always exactly two hours more than what it should be. Why would this happen? The only thing I can think of is that my timezone is GMT+2, but that doesn’t make sense either

Any help? I don’t want to just subtract 2 hours to make it work.

I’ve also tried various date formatting options, all the same result

Sounds like one of the parameters is incorrect. Try showing both in the report and see which one you need to take a closer look at.

I actually did that, the timestamps are fine, I wonder if it’s not maybe the fromMillis() instruction not factoring in the timezone, or something like that, the documentation states:

Creates a date object given a time, in milliseconds, past Unix epoch (1 January 1970 at midnight UTC).

It is factoring in time zone when you use fromMillis. I’d suggest scripting the duration.

Untested, but should get you close.

def hms(millisIn):
	seconds, ms = divmod(millisIn, 1000)
	minutes, seconds = divmod(seconds, 60)
	hours, minutes = divmod(minutes, 60)
	if hours > 24:
		days,hours = divmod(hours, 24)
		return '{}:{:02d}:{:02d}:{:02d}'.format(days,hours,minutes,seconds)
		return '{}:{:02d}:{:02d}'.format(hours,minutes,seconds)

startDate = data['StartDate']
endDate   = data['EndDate']

duration = endDate.getTime() - startDate.getTime()

data['duration'] =  hms(duration)

Use fromMillis() on each timestamp separately, and subtract them.

Like so:


fromMillis only works on dates/timestamps, not intervals.

1 Like

I appreciate the help, I will give it a go first thing tomorrow morning

I ended up going with this expression to get the time accurate and displayed correctly

dateFormat(fromMillis({EndDate}) - fromMillis({StartDate}) - fromMillis(getTimezoneRawOffset()*3600000),“HH:mm:ss”)

1 Like