Katalon Recorder 5.9.0
Windows 11 Enterprise
Chrome Version 120.0.6099.131 (Official Build) (32-bit)
How can I format this date ‘1/6/2003’ to ‘01/06/2003’ using JavaScript in Katalon Recorder? I have tried numerous formatting suggestions found in Stack Overflow without success. Any suggestions would be greatly appreciated.
Hi @grylion54, I am getting ‘(intermediate value).format is not a function’ exception when running this case. I am going to try a few things to see if I can find where I am going wrong.
[info] Playing test case newDate / Variables copy
[info] Time: Mon Jan 08 2024 09:00:56 GMT-0800 (Pacific Standard Time) Timestamp: 1704733256384
[info] OS: Windows Version: 10
[info] Browser: Chrome Version: 120.0
[info] If the test cannot start, please refresh the active browser tab
[info] Executing: | storeEval | var d=new Date(); new Date((d.getMonth()+1),(d.getDate()+1),(d.getFullYear()-21)); | birthDate |
[info] Store '1907-03-26T08:00:00.000Z' into 'birthDate'
[info] Executing: | echo | ${birthDate} | |
[info] Expand variable '${birthDate}' into '1907-03-26T08:00:00.000Z'
[info] echo: 1907-03-26T08:00:00.000Z
[info] Time: Mon Jan 08 2024 09:00:57 GMT-0800 (Pacific Standard Time) Timestamp: 1704733257920
[info] Test case passed
[info] Executing: | storeEval | var d=new Date(); new Date((d.getMonth()+1),(d.getDate()+1),(d.getFullYear()-21)).format("MM/dd/yyyy"); | birthDate |
[error] Threw an exception: (intermediate value).format is not a function
Maybe this horrific formula may do the trick (the formula at the link). What it’s doing is a tertiary statement to pad the date part if it is a single digit instead of a double digit. If it is a single digit it adds a zero to the front as a String. Yikes.
Thanks for all your help @grylion54 (Mike), We ended up using @kazurayam’s solution (thanks Kaz) as it works for all our needs… We can now create any date needed for our testing.
Cheers Dave
var currentDate = new Date(); var month = currentDate.getMonth() + 1; if (month < 10) { month = “0” + month }; var day = currentDate.getDate(); if (day < 10) { day = “0” + day }; var year = currentDate.getFullYear(); curentDate=(day + “/” +month + “/” + year);