No announcement yet.

Convert DOB to Age

  • Filter
  • Time
  • Show
Clear All
new posts

  • Convert DOB to Age

    Happy Monday everyone!

    I have done some digging and can't seem to find anything to use a transformer to convert a DOB to a number to reflect the actual age of the patient.

    This is for an ORM.
    I want to grab the DOB from PID.7 convert it to a number then create a single OBX line to include this information "PATIENT AGE: XX". There are no other OBX lines.

    Any thoughts?
    Mirth ver. (yes an upgrade is in order)

  • #2


    • #3
      You can also use JodaTime:

      var dob = org.joda.time.format.DateTimeFormat.forPattern('YYYYMMdd').parseDateTime(msg.PID['PID.7']['PID.7.1'].toString());'Patient Age: '+org.joda.time.Years.yearsBetween(dob,new org.joda.time.DateTime()).getYears());
      Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

      Nicholas Rupley
      Work: 949-237-6069
      Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.

      - How do I foo?
      - You just bar.


      • #4
        happy Monday to you from me and have a nice Monday and i think 2nd# user is saying rightly that You should also use JodaTime:Thanks
        Last edited by johnson33445; 11-25-2014, 02:33 AM.


        • #5
          Not sure I fully understood the question properly but this is my understanding of your question . How to use patient 'date of Birth' to estimate the current patient age. I have a requirement like this in the past and this is how I solved it below.

          1. First step : Create a some sort of Javascript function in Code Template and this should be available as user define function which you can call within the channel transformer or filter . You might need to modify the example of javscript below to tailor to your need .

          function calculatePersonAge(dateOfBirth)
          /* Format the Patient date of Birth as yyyy/MM/dd
          calling function method global variable and format the date input value:
          The date of birth must be in a 8 digits value same as HL7 format

          dateOfBirth = DateUtil.convertDate("yyyyMMdd", "yyyy/MM/dd", dateOfBirth); // Format date of birth

          var dob = new Date(dateOfBirth);
          // Capture the month patient born
          var Pmonth = dob.getMonth();
          // Set the day of the month patient born
          var Pdate = dob.getDate();
          // what year the patient born?
          var Pyear = dob.getFullYear();

          var today = new Date();
          var sys_date = today.getDate();
          var sys_Month = today.getMonth();
          var sys_Year = today.getFullYear();

          // Now, subtract the system and Person Dates

          var Age = sys_Year - Pyear;
          var theMonth = sysMonth - Pmonth;
          var theDate = sys_Date - Pdate;

          if (sysMonth < Pmonth || (sys_Month == Pmonth && sys_Date < Pdate)) Age--;
          // document.write("Your age today is :" + Age); Just for testing purpose
          return Age;
          // Log the error for human readable text
          logger.debug("Error occur " + ex + " " + "'(" + "at" + Date().now + "')");


          step 2

          Lastly you will need to call the function from your Reference Lists and pass the value of your PID.7 and that should return a actual integer value as a patient Age . Hope that help .
          Last edited by richmensp; 07-10-2013, 03:09 AM.