31 Dec 2018: Thank you to our knowledgeable and friendly forums users for another great year. We are aware of the uptick in spam accounts and are doing our best to ban these at first sight. Thank you for your patience!

# Calculating length of stay

Hello all.

I know that in OC we are able to calculate the difference between two date fields and the same for time fields, but is there a way to combine the two?

What I am interested in calculating the length of hospital stay (in hours and minutes) for a subject using date of admission, time of admission, date of discharge, and time of discharge.

I have attached a sample calculation in Excel.

Thank you,

O'Dane

• Hi O'Dane

Combine field is not present in OC for date and time. I saw excel which you attached here. You may need Java Script for that but all fields should be mandatory.
• Further to Csaba excellent script, here is another solution, which is based on the Date field in OpenClinica-form. For the rest it is basically the same, except for the output.

<div ID="Diff"></div>
<script src="includes/jmesa/jquery.min.js"></script>
<script>
\$.noConflict();
var fDate1 = \$("#Date1").parent().parent().find("input");
var fDate2 = \$("#Date2").parent().parent().find("input");
var fTime1 = \$("#Time1").parent().parent().find("input");
var fTime2 = \$("#Time2").parent().parent().find("input");
var fDiff = \$("#Diff").parent().parent().find("input");

function OCD2JD(OCD){
var dateParts = OCD.split("-");
var JDate = new Date();
JDate.setFullYear(dateParts);
JDate.setMonth(Calendar._SMN.indexOf(dateParts));
JDate.setDate(dateParts);
return JDate;
}
function H2M(TimeValue){
var HM = TimeValue.split(":");
if (HM){
var Hours = (1*HM) + (HM/60);
}
else{
var Hours = 0;
}
return Hours;
}

function DiffInHours(OCD1, OCD2){
var msecPerHour = 1000 * 60 * 60;
var interval = OCD2JD(OCD2).getTime() - OCD2JD(OCD1).getTime();
var hours = Math.floor(interval / msecPerHour );
if (isNaN(hours)){
return 0;
}
else{
return hours;
}
}

function calcDiff(){
var Diff = DiffInHours(fDate1.val(), fDate2.val()) + H2M(fTime2.val()) - H2M(fTime1.val());

var days = Math.floor(Diff/24);
if (days < 10){days = "0" + days};
Diff = Diff - (days * 24);
var hours = Math.floor(Diff);
if (hours < 10){hours = "0" + hours};
Diff = Diff - hours;
var minutes = Math.round(Diff * 60);
if (minutes < 10){minutes = "0" + minutes};
var CD = days + ":" + hours  + ":" + minutes;
if (fDiff.val() != CD){
fDiff.val(CD);
fDiff.change();
}
};
fDate1.blur(function(){
calcDiff();
});
fDate2.blur(function(){
calcDiff();
});
fTime1.keyup(function(){
calcDiff();
});
fTime2.keyup(function(){
calcDiff();
});
fDiff.blur(function(){
calcDiff();
})
fDiff.focus(function(){
calcDiff();
})
\$("#srl").click(function(){
calcDiff();
});
\$("#srh").click(function(){
calcDiff();
});
})
</script>
• Great. Thank you very much Gerben.
On Aug 20, 2014 8:53 AM, "GerbenRienk"
• Thank you Csaba and Gerben for your excellent solutions. Greatly appreciated.
This discussion has been closed.