Using executeScript_Sandbox to convert a date format not working RPA

I’m trying to use executeScript_Sandbox to convert a date format but the variable is not getting defined in UI.Vison. I’m wondering what I’m doing wrong.

{
“Name”: “change date”,
“CreationDate”: “2024-9-22”,
“Commands”: [
{
“Command”: “store”,
“Target”: “November 21, 2018”,
“Value”: “inputDate”,
“Description”: “”
},
{
“Command”: “executeScript_Sandbox”,
“Target”: “(function() { var inputDate = ‘${inputDate}’;\n var dateParts = inputDate.split(’ ‘); var year = dateParts[2].slice(-2);\n var monthNames = [‘January’,‘February’,‘March’,‘April’,‘May’,‘June’,‘July’,‘August’,‘September’,‘October’,‘November’,‘December’]; var month = (monthNames.indexOf(dateParts[0]) + 1);\n if (month < 10) { month = ‘0’ + month; } var day = dateParts[1].replace(’,', ‘’);\n if (day.length < 2) { day = ‘0’ + day; } \n return year + ‘.’ + month + ‘.’ + day; })();”,
“Value”: “formattedDate”,
“Description”: “”
},
{
“Command”: “echo”,
“Target”: “${formattedDate} - formated date”,
“Value”: “”,
“Description”: “dd”
}
]
}


        return year + '.' + month + '.' + day; })(); | formattedDate | 

[info]
Executing: | echo | ${formattedDate} - formated date | |
[echo] undefined - formated date

Thank You

type or paste {
  "Name": "change date",
  "CreationDate": "2024-9-22",
  "Commands": [
    {
      "Command": "store",
      "Target": "November 21, 2018",
      "Value": "inputDate",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "(function() { var inputDate = '${inputDate}';\n             var dateParts = inputDate.split(' '); var year = dateParts[2].slice(-2);\n             var monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']; var month = (monthNames.indexOf(dateParts[0]) + 1);\n             if (month < 10) { month = '0' + month; } var day = dateParts[1].replace(',', '');\n             if (day.length < 2) { day = '0' + day; } \n             return year + '.' + month + '.' + day; })();",
      "Value": "formattedDate",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${formattedDate} - formated date",
      "Value": "",
      "Description": "dd"
    }
  ]
}here

the key issue is that you must not use the function notation. Remove the (function (){ } part and then code runs:

image

I think there is still another issue in the code, but to fix this I would need to know what the desired result is.

1 Like

Hello Plankton, thank you for your assistance. it is just a code snippet I’ve created for proof of concept or a bit of practice before implementation of a solution.

In this snippet I’m converting a date in the format “November 21, 2018” to the format 21.11.08" and placing the result in a variable for use elsewhere in a Macro that hasn’t been constructed yet.

Thank you.

with a bit more research I was able to locate and test a code snippet elsewhere on the Forum.
This simpler bit of code has resolved my issue.

type or paste {
  "Name": "creationDate",
  "CreationDate": "2024-9-23",
  "Commands": [
    {
      "Command": "store",
      "Target": "November 21, 2018",
      "Value": "thisDate",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var d = new Date(${thisDate});var m = ((d.getMonth()+1)<10) ? \"0\" + (d.getMonth()+1):(d.getMonth()+1);var day=d.getDate()<10 ? \"0\" + d.getDate():d.getDate();return day + \".\" + m + \".\" + d.getFullYear();",
      "Value": "date",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${date}",
      "Value": "",
      "Description": ""
    }
  ]
}
==============================

info]
Executing:  | store | November 21, 2018 | thisDate | 
[info]
Executing:  | executeScript_Sandbox | var d = new Date(${thisDate});var m = ((d.getMonth()+1)<10) ? "0" + (d.getMonth()+1):(d.getMonth()+1);var day=d.getDate()<10 ? "0" + d.getDate():d.getDate();return day + "." + m + "." + d.getFullYear(); | date | 
[info]
Executing:  | echo | ${date} |  | 
[echo]
21.11.2018

thanks
1 Like