javascriptdatekatalon-recorderkatalon

Get today's date, minus 28 days, in Katalon Recorder


My question is specific to Katalon Recorder, for which certain examples of JavaScript do not work, see associated picture.

I am using Katalon Recorder and need to verify values in two fields in a search modal pop-up. The commands to put today's date into a variable (todaysdate) and verify the Today's Date field matches is working just fine. But I'm not sure how to use the same javascript to get today's date minus 28 days to verify the value in the From Date field.

I've attached a picture showing the section that works for Today's Date on the left with a picture of the modal on the right. Is there a way for me to use the same new Date().toLocaleDateString to get today's date minus 28 days?

enter image description here


Solution

  • new Date(new Date().setDate(new Date().getDate() - 28)).toLocaleDateString('en-us', {month: '2-digit', day: '2-digit', year: 'numeric'}); is strange looking but it works. If we break it down the inner most part is obvious new Date().getDate() - 28 to get 28 days before today and setting that to a date new Date().setDate(new Date().getDate() - 28) The problem is date.setDate() returns a number rather than a date. Hence the final outer new date new Date(new Date().setDate(new Date().getDate() - 28))