Difficulty converting date, please assist [Solved by ChatGPT4 :-) ]

I am trying to take dob and a date to work out the age. The check if that age is in the array c1ecg. I can get all the parts working except changing my birthday variable from dd/mm/yyyy to yyyy/mm/dd to calculate the age properly. The script doesnt error but the second brithday2 remains undefined when running it.

{
  "Name": "Aviation",
  "CreationDate": "2024-2-25",
  "Commands": [
    {
      "Command": "store",
      "Target": "40\t45\t50\t52\t54\t56\t58\t60\t62\t64\t66\t68\t70\t71\t72\t73\t74\t75\t76\t77\t78\t79\t80\t81\t82\t83\t84\t85\t86\t87\t88\t89\t90\t91\t92\t93\t94\t95\t96\t97\t98\t99\t100",
      "Value": "c1ecg",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "13/05/1988",
      "Value": "birthday",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "\nvar newdate = ${birthday}.split(\"/\").reverse().join(\"-\");",
      "Value": "birthday2",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "function getAge(dateString) {var today = new Date();var birthDate = new Date(dateString);var age = today.getFullYear() - birthDate.getFullYear();var m = today.getMonth() - birthDate.getMonth();if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())){age--;}return age;}return getAge(${birthday});",
      "Value": "age",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${age}",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "if",
      "Target": "${c1ecg}.lastIndexOf(${age}) > -1",
      "Value": "",
      "Description": "the return value of .lastIndexOf() is -1 if the substring is not found in the string at all."
    },
    {
      "Command": "echo",
      "Target": "yes, it contains this string",
      "Value": "blue",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}

Interesting question. But did you know that ChatGPT can answer Ui.Vision questions, too? :grinning:

Especially for questions with regards to Javascript and executeScript/executeScript_Sandbox ChatGPT4 works very well.

:point_right: Here is my short UI.Vision ChatGPT4 chat :star_struck:

Gemini also works, but is not yet as good as ChatGPT4 for Ui.Vision RPA questions

ChatGPT4 Solution (bug found and fixed):

{
  "Name": "bday",
  "CreationDate": "2024-2-27",
  "Commands": [
    {
      "Command": "store",
      "Target": "40\t45\t50\t52\t54\t56\t58\t60\t62\t64\t66\t68\t70\t71\t72\t73\t74\t75\t76\t77\t78\t79\t80\t81\t82\t83\t84\t85\t86\t87\t88\t89\t90\t91\t92\t93\t94\t95\t96\t97\t98\t99\t100",
      "Value": "c1ecg",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "13/05/1988",
      "Value": "birthday",
      "Description": ""
    },
    {
      "Command": "comment",
      "Target": "executeScript_Sandbox // \nvar newdate = ${birthday}.split(\"/\").reverse().join(\"-\");",
      "Value": "birthday2",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var dateString = '${birthday}'.replace(/\"/g, ''); var dateParts = dateString.split('/'); var newdate = dateParts[2] + '/' + dateParts[1] + '/' + dateParts[0]; return newdate;",
      "Value": "birthday2",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "function getAge(dateString) {var today = new Date();var birthDate = new Date(dateString);var age = today.getFullYear() - birthDate.getFullYear();var m = today.getMonth() - birthDate.getMonth();if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())){age--;}return age;}return getAge('${birthday2}');",
      "Value": "age",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${age}",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "if",
      "Target": "${c1ecg}.lastIndexOf(${age}) > -1",
      "Value": "",
      "Description": "the return value of .lastIndexOf() is -1 if the substring is not found in the string at all."
    },
    {
      "Command": "echo",
      "Target": "yes, it contains this string",
      "Value": "blue",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}
2 Likes