Handling date form variable in JavaScript Script Task


What would be the correct way to handle process variable of type Date (filled on the Start Process form) mapped to a json field in a JavaScript Script Task?
Regular assignment like json.TestDate = testDate; causes the TestDate to disappear completely from the new variable, when I execute the code:

var json = {};
json.TestDateString = testDate.toString();
json.TestDate = testDate;
json.TestDateJs = new Date();
json.TestString = testString;
execution.setVariable("asJson", S(JSON.stringify(json)));
execution.setVariable("asString", JSON.stringify(json));

Value of asString variable:

"TestDateString":"Wed Jun 23 00:00:00 CEST 2021",

I’ve tried using replacer or new Date(startDate).toISOString() or new Date(startDate.toString()) but to no avail.
I’m using Camunda Platform Community v7.14 with Java 8.

I know that Script Tasks are not the best way to use Camunda, but doing it in a Java delegate or an external task is not an option for me.

I would greatly appreciate some help with this.

Example model attached:
date_stringify_issue.bpmn (4.7 KB)

1 Like

@eileen Variables are type of “Date” can be saved as Date type itself.

execution.setVariable("date", new Date())

Json format can be used if we want to serialize the object and store as json payload.