how do i convert a date to utc then format it out as a string

0 votes
asked Sep 16, 2021 by nickie-l-1757 (200 points)
i tried using format of 'yyyy-MM-ddThh:mm:ssZ' but it doesn't actually perform the utc conversion

1 Answer

0 votes

The format date function will output the date based on the server timezone.  There is not 'automatic' conversion to UTC.  However, the script to do this is simple.  There are a couple of examples in the KB already (see: or ).

But basically, you will take your date string (wherever you got it) and run the following:

// first convert your date value from a string to a date object in java
var qieDate = qie.deduceDate(dateString);

// now we need to output the date as a formatted UTC date
var utcTimeFormat = new java.text.SimpleDateFormat('yyyy-MM-dd HH:mm:ss.SSSZ');

// now tell the new SimpleDateFormat object that you want the UTC timezone

// the call to format will do the conversion and output the date in the desired format
var utcDate = utcTimeFormat.format(qieDate);

// utcDate can be placed in the message and the receiving system can process it as UTC

answered Sep 16, 2021 by ben-s-7515 (12,640 points)
commented Sep 16, 2021 by mike-r-7535 (13,830 points)
I have seen some "required" formats where they need the literal 'Z' character at the end to represent it is in UTC.  In that case, you could put single quotes (') around the Z like this:
var utcTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'Z'");

To produce something like this: 2021-09-01 14:00:00.000Z

This only makes sense if you are setting the TimeZone to UTC as this example is doing.