Mirth (NextGen) Connect Bits #4: How To Use DateUtil Class

DateUtil is a powerful function found in the Mirth Connect application. When calling it in a JavaScript type Filter/Transformer, you should see the definition and arguments needed.

Mirth Connect DateUtil JavaScript Description

You can also find it in the “Reference” section.

Mirth Connect DateUtil Reference page.

This is part of the User API in the application, you can check the documentation here: DateUtil Class.

  • convertDate –  Parses a date string according to a specified input pattern, and formats the date back to a string according to a specified output pattern.
    • Accepted arguments and data type are the followings:
      • Input pattern date format or the format of the source date. Data type should be a String.
      • Output pattern date format or the output format of the date. Data type should be a String.
      • Input date or the actual date string to be converted. Data type should be a String.
    • Returns a date string value
  • formatDate – Formats a java.util.Date object into a string according to a specified pattern.
    • Accepted arguments and data type are the followings:
      • Output pattern date format or the format of the source date. Data type should be a String.
      • Date to be formatted. Data type is an object.
    • Returns a date string value
  • getCurrentDate – Formats the current date into a string according to a specified pattern.
    • Accepted arguments and data type are the followings:
      • The output date pattern. Data type should be a String.
    • Returns a date string value
  • getDate – Parses a date string according to the specified pattern and returns a java.util.Date object.
    • Accepted arguments and data type are the followings:
      • Input pattern date format or the format of the source date. Data type should be a String.
      • Actual date value. Data type should be a String.
    • Returns a date object

Here are the date format characters from the Mirth Tutorial – Working With Dates. You’ll need this when you want to define or tell the method how your date string is currently formatted or how you want the output to look like:

  • G – Era Text (e.g. AD)
  • y – Year
  • M – Month
  • w – Week (in the year)
  • W – Week (in the month)
  • D – Day (in the year)
  • d – Day (in the month)
  • F – Day (of the week; numeric)
  • E – Day (of the week; word)
  • a – AM/PM
  • H – Hour (0-23)
  • k – Hour (1-24)
  • K – Hour (0-11)
  • h – Hour (1-12)
  • m – Minute
  • s – Second
  • S – Millisecond
  • z – Time zone
  • Z – Time zone (different formatting) 

Using DateUtil.convertDate

Below is a sample usage of the method converting a date string to another format.

var dateString = '09/01/2020 11:00 AM';
var dateFormatted = DateUtil.convertDate('MM/dd/yyyy hh:mm a', 'yyyy-MM-dd hh:mm a', dateString);

logger.info(dateFormatted); // returns 2020-09-01 11:00 AM

Here’s another one where I’m pulling the date from the PID.7.1 field which is the date of birth of the patient.

var dateString = msg['PID']['PID.7']['PID.7.1'].toString(); // patient DOB in HL7 message with the usual format yyyyMMdd
var dateFormatted = DateUtil.convertDate('yyyyMMdd', 'yyyy-MM-dd HH:mm', dateString);

logger.info(dateFormatted);

DateUtil.formatDate

Here’s a sample usage where we format a date object (current date) to a date string. Instances where you need this is if you need to make changes or use date logic like checking if the date is before or after a certain date, before you format it to a date string. Getting parts of a date or getting the date differences.

var dateObject = new Date();

var dateFormatted = DateUtil.formatDate('MM/dd/yyyy HH:mm', dateObject);

logger.info(dateFormatted); // returns a date string with specific format

DateUtil.getCurrentDate

Here we get the current date with a argument on how we want it to format. Pretty straight forward.

var dateToday = DateUtil.getCurrentDate('yyyy-MM-dd HH:mm');

logger.info(dateToday); // return date string today

DateUtil.getDate

Here the getDate creates a date object based on the input pattern and the actual source of date string. In my case, it’s the OBR.7.1 which is the observation date & time. With date objects, you can call on the methods like .after or .before.

var dateObject = DateUtil.getDate('yyyyMMddHHmmss', msg['OBR']['OBR.7']['OBR.7.1'].toString());

logger.info(dateObject.toLocaleString()); // returns date formatted using default locale

logger.info(dateObject.after(new Date())); // checks if date object is after the current date

logger.info(dateObject.before(new Date())); // checks if date object is before the current date

Working on Date Object

With the date object, you can also perform like date difference checks or change the date to a later one.

var dobObject = DateUtil.getDate('yyyyMMdd', msg['PID']['PID.7']['PID.7.1'].toString()); // patient dob as a date object
var currentDate = new Date();

var diff = currentDate.getTime() - dobObject.getTime(); // get the date difference in milliseconds between current date and dob

var age = diff / 1000 / 60 / 60 / 24 / 365; // get number of years

logger.info(age);

Here’s another sample code where we get the date yesterday.

var yesterday = new Date(); // gets current date

yesterday.setDate(yesterday.getDate() - 1); // deduct 1 day to current date

var dateString = DateUtil.formatDate('yyyy-MM-dd', yesterday); // convert to a readable date

logger.info(dateString);

Note that as per W3School’s Date Set Methods, “If adding days shifts the month or year, the changes are handled automatically by the Date object.

You can check Mozilla Developer page for the methods on a JavaScript Date Object.

For more Mirth Connect related blog posts, check out the Mirth (NextGen) Bits tag or Health IT page. Feel free to leave a comment or feedback.

Cheers!

Add a Comment

Your email address will not be published. Required fields are marked *