Cannot create Date variable using Javascript in script task

Hi. Thanks In advance to anyone who is willing to help me.

I am trying to create a date variable using a script task and JavaScript. The code I have is as follows:

var date1= new Date(“2016-01-25T13:33:42”);

However when I try to inspect the variable “date1” in cockpit under “process instances” ->“variables” the variable looks as follows (under the serialized tab its empty i.e. {}):

Where have I gone wrong and how can I create a variable of type “Date” using a smilar process?

Thanks again

I think new Date(“2016-01-25T13:33:4”); is incorrect JS date format.
look at date standard MDN Date

Hello kdgeyser.

Your code seems to work indeed before the setVariable (for Nur_tem : you forgot to add the latest digit).

After deployment of version 7.8 I had plenty of Date issues even in Groovy scripts.

My suggestion is, right now, to try Groovy ans set a direct Java Date object (You still will have problems with forms).

Another workaroud is to use strings, so you can store the variable as a string and convert to a real date only when you need it (this could solve a lot of issues with forms).

1 Like

I dont think that the problem as it gives me the same result if I use either of the following:

var date1= new Date();
var date1= new Date(“2016-01-25T13:33:42.165+0100”);
var date1= new Date(“2016-01-25T13:33:42.165Z”);

1 Like


Thanks so much for the response.

I have used some groovy scripts before with some success so I will try that too. With regards to your suggestion on having the variable as a string that is actually exactly what I am trying to do.

I am currently entering the following JSON object via a user form (stored as “tc_input”):
“date1”: “2016-01-25T13:33:42”,
“date2”: “2018-05-05T01:00:00”

I then try to convert date1 into a date object as follows (in a script task - using Javascript):

var tc_input=execution.getVariable(“tc_input”);
var tc_input_json=S(tc_input);
var date1_string= tc_input_json.prop(“date1”).value();
var date1= new Date(date1_string);

It all works(i.e it creates the process variable date1_string) but ends up giving me the exact same error as in the original post? Any ideas or is this a bug that needs to be sorted and I should just try using groovy?

I believe this is the explanation: Any Benefits of using Spin over Javascript Objects when using javascript Scripting. Make sure to create an instance of java.util.Date, not a Javascript Date.


1 Like

Yes. Using java.util.Date instead of Date could be the solution if not using Spin.

Using util.Date forces you to make a parsing of the original date though.

Thanks so much for the responses that really did help a lot with understanding the problem.

Fyi I managed to get it to work using a groovy script task and changing the input date format to “yyyy-MM-dd”:

def tc_input_json=S(tc_input);
execution.setVariable(“tc_input_json”, tc_input_json);

def date1_string= tc_input_json.prop(“date1”).value();

def date1 = new Date().parse(“yyyy-MM-dd”,date1_string);
execution.setVariable(“date1”, date1);

1 Like