web stats
Get date/timestamp from NTE - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 03-25-2020, 08:47 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 109
jg12345 is on a distinguished road
Default Get date/timestamp from NTE

I created logic below to look in the NTE segment and get the Collected, Received and Reported date/timestamp assuming the month, day and hour are each two digits. I'm finding now the sending system does not always send two digits for month, day or hour, rather it could be one digit each. I'm having a problem checking if month, day, hour is 1 digit to add a preceding zero. Or maybe there is a better way to handle this?

Code:
if (msg['OBR'][0]['OBR.4']['OBR.4.1'].toString() == '12345') {

	for (var i=msg['NTE'].length()-1; i >= 0;i--) {

	var date = msg['NTE'][i]['NTE.3']['NTE.3.1'].toString().substr(0,14);
	var actualDate = msg['NTE'][i]['NTE.3']['NTE.3.1'].toString().substr(15,29);

		if (date == 'Date Collected') {
			var month = actualDate.substr(1,2);
			var day = actualDate.substr(4,2);
			var year = actualDate.substr(7,4);
			var time = actualDate.replace(':', '').substr(12,5) + '00';
			msg['OBR'][0]['OBR.7']['OBR.7.1'] = year + month + day + time;
		}

		if (date == 'Date Received:') {
			var month = actualDate.substr(0,2);
			var day = actualDate.substr(3,2);
			var year = actualDate.substr(6,4);
			var time = actualDate.replace(':', '').substr(11,5) + '00';
			msg['OBR'][0]['OBR.14']['OBR.14.1'] = year + month + day + time;
		}

		if (date == 'Date Reported:') {
			var month = actualDate.substr(0,2);
			var day = actualDate.substr(3,2);
			var year = actualDate.substr(6,4);
			var time = actualDate.replace(':', '').substr(11,5) + '00';
			msg['OBR'][0]['OBR.22']['OBR.22.1'] = year + month + day + time;
		}
	}
}
Sample HL7 works with logic above
Code:
MSH|^~\&|
PID|1|
PV1|1|
ORC|RE|
OBR|1|||12345^TEST|
OBX|1|
NTE|1|
NTE|2||Accession Number: 99-MG-20-078512
NTE|3||Date Collected: 03/12/2020 10:24
NTE|4||Date Received: 03/13/2020 02:25
NTE|5||Date Reported: 03/19/2020 19:11
Sample HL7 below does not work since month, day and hour are 1 digit
Code:
MSH|^~\&|
PID|1|
PV1|1|
ORC|RE|
OBR|1|||12345^TEST|
OBX|1|
NTE|1|
NTE|2||Accession Number: 99-MG-20-078512
NTE|3||Date Collected: 3/12/2020 10:24
NTE|4||Date Received: 03/5/2020 02:25
NTE|5||Date Reported: 03/19/2020 9:11
Reply With Quote
  #2  
Old 03-25-2020, 10:34 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,360
cory_cole is on a distinguished road
Default

var datePieces = actualDate.split('/');

var month = datePieces[0];
var day = datePieces[1];
var year = datePieces[2]
Reply With Quote
  #3  
Old 03-25-2020, 12:47 PM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 109
jg12345 is on a distinguished road
Default

That does the trick... Thanks Cory!
Reply With Quote
  #4  
Old 03-25-2020, 02:14 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,165
agermano is on a distinguished road
Default

Not sure if it's intentional or not, but you have a single digit hour in your example, too.

Using a basic split won't zero pad your output, which I'm guessing is needed. You can use the java.time package to handle all of this for you (also the time component.)

Code:
var inboundDateFormat = java.time.format.DateTimeFormatter.ofPattern('M/d/yyyy H:mm');
var outboundDateFormat = java.time.format.DateTimeFormatter.ofPattern('yyyyMMddHHmm');

var ntePattern = /^Date (\w*): (\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d\d)$/;

if (msg['OBR'][0]['OBR.4']['OBR.4.1'].toString() == '12345') {
    for each (var nte3 in msg.NTE['NTE.3']['NTE.3.1']) {
        var match = nte3.toString().match(ntePattern);
        if (match) {
            var outDate = java.time.LocalDateTime.parse(match[2], inboundDateFormat).format(outboundDateFormat);
            switch(match[1]) {
                case 'Collected':
                    msg['OBR'][0]['OBR.7']['OBR.7.1'] = outDate;
                    break;
                case 'Received':
                    msg['OBR'][0]['OBR.14']['OBR.14.1'] = outDate;
                    break;
                case 'Reported':
                    msg['OBR'][0]['OBR.22']['OBR.22.1'] = outDate;
                    break;
            }
        }
    }
}
Reply With Quote
  #5  
Old 03-26-2020, 06:07 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 109
jg12345 is on a distinguished road
Default

Yes, single digit hour was intentional I was planning to handle it by checking the length of each split. Your solution totally simplifies the logic.

Thank you so much!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 05:57 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation