Calculating minutes between two dates

I am trying to use the d2:minutesBetween expression to calculate the minutes between clinical arrival date and when XX happens. They are both type = datetime as stipulated in the documentation.

However, when trying to use this formula, it is giving me an error, while d2:daysBetween works. Is it the case that the minutes version has a bug? Has anyone else experienced this issue? Has anyone else used d2:minutesBetween succesfully? I cannot find any additional guidance online.

Here is a screenshot below. Thanks in advance for any guidance! Just seems odd to get this error, as the structure of the function is pretty straightforward.

Sara

1 Like

Hi Sara,

Did you try to save the indicator and run analytics?

As far as I understand, dhis2 analytics uses different parsers to validate and evaluate expressions. This means that in some versions, an expression may be validated as “not valid” in the UI, but would still actually be calculated.

For example, d2:minutesBetween() works in 2.32 demo, but not 2.29 demo

Luckily, @Jim_Grace is working on a system to unify validation and evaluate across validation rules, program rules, and program indicators with a common ANTLR expression grammar https://jira.dhis2.org/browse/DHIS2-4469

4 Likes

Hi @brian many thanks for your reply. I did try saving and running analytics and it is still not working…I am just getting false as the indicator output.

If anyone else has any ideas of how to find the minutes between two datetime variables, without having to create a separate variable for hour and minute, please let me know.

1 Like

Hi Sara!
Which version of DHIS2 are you running?

UPDATE: Reading in another thread that the problem is on 2.30.

Markus

2 Likes

Hi Markus,

Yes, I am using 2.30 - any advice for a work-around?

2 Likes

Hi, everyone! I’m having the same issue Sara was having. I need to compute the time difference in minutes between 2 data elements with data and time as the value type in 2.30. I tried d2:minutesBetween() but I’m getting the same error which is Expression is not valid. Upon checking, d2:minutesBetween() does not work in 2.30. Does anybody have any advice on a possible workaround without having to create new data element/s?

Thank you in advance!

Hey @rose indeed there’s a problem with this function in 2.30 and I would say beyond that: https://jira.dhis2.org/browse/DHIS2-6038

We had the same problem for a programRUle that assigned a value to a DE. I don’t know if what I am about to write can also work in programIndicators. The workaround we did was not really straightforward but working for us (although we have now problems updated 2.33)

We created a custom javascript function like the one below and incorporate its definition in the scripts.js

function difference_time_minutes(date1, date2) {
start_actual_time = new Date(date1).getTime();
end_actual_time = new Date(date2).getTime();
//alert( date1);
//alert(date2);
var diff = end_actual_time - start_actual_time;

var diffSeconds = diff / 1000;

var MM = (diffSeconds) / 60;

//alert(MM);
return MM;}

However this is no longer recognized in the new capture app. We are trying to use again the d2:minutesbetween function in 2.33 but still facing the same challenges. Hope that anyone can advice us better.

Thanks

Hi @barreda. Thanks for the response. Sorry was not able to update this recently but it seems like a front-end issue (?) because when I tested the indicator against data it was working fine (though it was showing error in the program indicator interface). See below screenshot of the chart using d2:minutesBetween() expression and the value type is date and time in 2.30. Maybe @Scott or @Markus can help clarify?

For the program rule, I wanted to show a warning for example arrival data and time should be before vitals date and time. I tried d2:minutesBetween() expression, and yes it did not work for me also and the expression that worked for me is #{ArrivalDateTime} > #{VitalsDateTime}. This works in Event Capture in 2.30 as well.

Hope this helps!

1 Like