Search code examples
dateutcdate-conversionmirth

How to convert local date to UTC in Mirth?


In Mirth I receive a local datetime string (201801011000) which I need to convert to UTC. I soon found out using the classic js new Date() doesn't work well.

This for example:

var d = new Date("2018-01-01 10:00");
logger.info(d.toString());

gives me an Invalid Date.

So after some more searching I found I can do this:

var d = DateUtil.getDate("yyyyMMddHHmm", "201801011000");

and from here I'm stuck. I don't know how I can convert this to UTC. Local server timezone is assumed which is enough for now, but in the future I also need to set a specific non-local timezone.

I tried to get the methods I can use with Object.getOwnPropertyNames(d), but that gives me the helpfull TypeError: Expected argument of type object, but instead had type object

I also tried looking up the Java docs for DateUtil and tried some methods from that, but nothing worked.

How can I convert datestring from local time to UTC?


Solution

  • Ok, after messing around with this for about two full days I finally found a solution. In the end I had to tap into Java, but since I couldn't import any java dependencies I had to use their direct class path (e.g.: java.text.SimpleDateFormat).

    In the end this is what worked for me:

    var datestr = "201207011000".slice(0, 12);  // This is just a datetime string to test with
    
    var formatter_hl7 = new java.text.SimpleDateFormat("yyyyMMddHHmm");
    formatter_hl7.setTimeZone(java.util.TimeZone.getTimeZone("CET"));
    var formatter_utc = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
    formatter_utc.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));
    
    var date_in_utc = formatter_utc.format(formatter_hl7.parse(date_str));
    

    Regardless, I wish you all a beautiful day!