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

Best Answer

Answers

  • anandRanandR Posts: 3
    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.
  • GerbenRienkGerbenRienk Posts: 793 ✭✭✭
    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();
    jQuery(document).ready(function($) { 
    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[2]);
     JDate.setMonth(Calendar._SMN.indexOf(dateParts[1]));
     JDate.setDate(dateParts[0]);
     return JDate;
     }
    function H2M(TimeValue){
      var HM = TimeValue.split(":");
      if (HM[1]){
      var Hours = (1*HM[0]) + (HM[1]/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>
  • odaneodane Posts: 9
    via Email
    Great. Thank you very much Gerben.
    On Aug 20, 2014 8:53 AM, "GerbenRienk"
  • odaneodane Posts: 9
    Thank you Csaba and Gerben for your excellent solutions. Greatly appreciated.
This discussion has been closed.