How can I format this date '1/6/2003' to '01/06/2003' using JavaScript in Katalon Recorder?

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.

storeEval | var d=new Date(); (d.getMonth()+1)+'/'+((d.getDate()+1))+'/'+(d.getFullYear()-21); | birthDate
echo | ${birthDate} | 

Log file from Katalon Recorder:
[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(); (d.getMonth()+1)+‘/’+((d.getDate()+1))+‘/’+(d.getFullYear()-21); | birthDate |
[info] Store ‘1/6/2003’ into ‘birthDate’
[info] Executing: | echo | ${birthDate} | |
[info] Expand variable ‘${birthDate}’ into ‘1/6/2003’
[info] echo: 1/6/2003
[info] Time: Fri Jan 05 2024 15:00:17 GMT-0800 (Pacific Standard Time) Timestamp: 1704495617624
[info] Test case passed

My result = 1/6/2003, looking to have it display as 01/06/2003

1 Like

How about try to put parentheses around the date so we include all date parts and then give it that format, like below:

storeEval | var d=new Date(); new Date((d.getMonth()+1),(d.getDate()+1),(d.getFullYear()-21)).format("MM/dd/yyyy"); | birthDate

Edit: And I found this thread:

1 Like

Thanks Mike, Good idea, will give it a try.

You should use Intl.DateTimeFormat object to format a Date object in your favorite way:

Example:

const dt = new Date(2003,5,1,0,0,0,0);
const fmt = new Intl.DateTimeFormat('en-GB', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
})
console.log(fmt.format(dt))

Output:

01/06/2003
1 Like

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 @kazurayam , I tried doing this earlier without success. I must have had a syntax error in my code. I will try you suggestion again tomorrow.

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

Creates 20 year old birthdate:

storeEval | var d=new Date(); (d.getFullYear()-20); | presentYear
storeEval | var d=new Date(); (d.getDate()); | presentDay
storeEval | var d=new Date(${presentYear},${presentDay},1,0,0,0,0);const fmt = new Intl.DateTimeFormat('en-GB', {year:'numeric',month:'2-digit',day:'2-digit'});fmt.format(d) | birthDate
echo | Birth-Date Format MM/DD/YYYY: ${birthDate} | 
//Output: Birth-Date Format MM/DD/YYYY: 01/10/2004

Creates 5 year expiration date:

storeEval | var d=new Date(); (d.getFullYear()+5); | presentYear
storeEval | var d=new Date(); (d.getDate()); | presentDay
storeEval | var d=new Date(${presentYear},${presentDay},1,0,0,0,0);const fmt = new Intl.DateTimeFormat('en-GB', {year:'numeric',month:'2-digit',day:'2-digit'});fmt.format(d) | expiryDate
echo | Expiry-Date Format MM/DD/YYYY: ${expiryDate} | 
//Output: Expiry-Date Format MM/DD/YYYY: 01/10/2029

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);

2 Likes