title: In mirth connect convert hl7 datetime in local time to iso date in UTC
body: <p>I have the following code that parses fields from an HL7 2.1 message, it converts it to a JSON object for further processing in Logstash/ElasticSearch/Kibana my issue is how to correctly parse the date-time stamps from HL7 local time to a UTC ISO8691 date time in a transformer. I have included the entire code of the transformer. </p>
  
  <p>The ConvertToISODate function is a hack to simply create a ISO Date time string, but it doesn't convert it to UTC, which I need.</p>
  
  <pre><code>function ConvertToISODate(hl7Date) {
      // this function will return an ISO formatted date string from the insanity that is hl7 dates
      // this function is also insane but the sane ways didn't seem to work
      var outdate = '';
      //4 year
      outdate += hl7Date.substring(0,4) + '-';
      //2 month
      outdate += hl7Date.substring(4,6) + '-';
      //2 day
      outdate += hl7Date.substring(6,8) + 'T';
      //2 hour
      outdate += hl7Date.substring(8,10) + ':';
      //2 minute
      outdate += hl7Date.substring(10,12) + ':';
      //2 second assume start of minute as not precise
      outdate += '00.000Z';
      return outdate;
  
  }
  
  function Group(obrseg) {
      this.seqno = obrseg['OBR.1']['OBR.1.1'].toString();
      if (this.seqno == '') {
          this.seqno = '1';
      } else {
          this.seqno = this.seqno;
      }
      this.reptno = obrseg['OBR.3']['OBR.3.1'].toString();
      this.reqno = obrseg['OBR.3']['OBR.3.1'].toString().substr(0, 10);
      this.test = obrseg['OBR.4']['OBR.4.1'].toString(); // TODO what about s/a's ?
      this.groupname = obrseg['OBR.4']['OBR.4.2'].toString();
      this.priority = obrseg['OBR.5']['OBR.5.1'].toString();
      this.reg_dt = ConvertToISODate(obrseg['OBR.6']['OBR.6.1'].toString());
      this.requester_id = obrseg['OBR.16']['OBR.16.1'].toString();
      this.requester_surname = obrseg['OBR.16']['OBR.16.2'].toString();
      this.requester_firstname = obrseg['OBR.16']['OBR.16.3'].toString();
      this.rept_dt = ConvertToISODate(obrseg['OBR.22']['OBR.22.1'].toString());
      this.status = obrseg['OBR.25']['OBR.25.1'].toString();
  }
  function Constit(obxseg) {
      this.seqno = obxseg['OBX.1']['OBX.1.1'].toString();
      if (this.seqno == '') {
          this.seqno = '1';
      } else {
          this.seqno = this.seqno;
      }
      this.valtype =obxseg['OBX.2']['OBX.2.1'].toString();
      this.test = obxseg['OBX.3']['OBX.3.1'].toString(); // TODO what about s/a's ?
      this.testname = obxseg['OBX.3']['OBX.3.2'].toString();
      this.result = obxseg['OBX.5']['OBX.5.1'].toString();
      this.abn_flags = obxseg['OBX.8']['OBX.8.1'].toString();
      this.status = obxseg['OBX.11']['OBX.11.1'].toString();
  }
  // FIXME what about NTE ???
  
  var curGroup = null;
  var lastReqno = null;
  // this is the main output string
  var eventOut = {};
  // eventOut.obr = {};
  var obrIndex = 0;
  // get the orginating lab from the msh header
  eventOut["originatinglab"] = msg['MSH']['MSH.4']['MSH.4.1'].toString();
  eventOut.groups = [];
  for each (seg in msg.children()) {
      switch(seg.name().toString()) {
          case "OBR":
              curGroup = new Group(seg);
              if (lastReqno == null || lastReqno != curGroup.reqno) {
                  lastReqno = curGroup.reqno;
              }
              // add current group to eventOut Object
  
              eventOut["reptno"] = curGroup.reptno;
              //eventOut.obr[curGroup.seqno] = curGroup;
              //eventOut.obr[curGroup.seqno]["obx"] = {};
              eventOut.groups[obrIndex] = curGroup;
              eventOut.groups[obrIndex++]["tests"] = [];
              var obxIndex = 0;
              break;
          case "OBX":
              var constit = new Constit(seg);
              //eventOut.obr[curGroup.seqno]["obx"][constit.seqno] = constit;
              eventOut.groups[obrIndex - 1]["tests"][obxIndex++] = constit;
              break;
          default:
              break; // ignore
      }
  }
  
      // now to serilize the object and save to the channel map
      // var eventOutString = JSON.stringify(eventOut);
  
  json = JSON.stringify(eventOut, null); // Convert XML HL7 to json don't pretty print
  
  // json = JSON.parse(json);  // turn it back into a javascript object
  // json.orignal_message = original.toString();
  // json = JSON.stringify(json);
  
  channelMap.put('xml', msg);
  channelMap.put('json', json);
  </code></pre>