Announcement

Collapse
No announcement yet.

Get date/timestamp from NTE

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    var datePieces = actualDate.split('/');

    var month = datePieces[0];
    var day = datePieces[1];
    var year = datePieces[2]

    Comment


    • #3
      That does the trick... Thanks Cory!

      Comment


      • #4
        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;
                    }
                }
            }
        }

        Comment


        • #5
          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!

          Comment

          Working...
          X