0

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

BigRedEO
  • 807
  • 4
  • 13
  • 33
  • 1
    maybe something wacky like this `new Date(new Date().setDate(new Date().getDate() - 28)).toLocaleDateString('en-us', {month: '2-digit', day: '2-digit', year: 'numeric'});` – Portal Mar 08 '23 at 21:24
  • @Portal - Will try when I get a chance tomorrow - I had something close to this, but it was a no-go. Using the new Date inside the () may be what I need here! Thank you! – BigRedEO Mar 08 '23 at 21:34
  • The *Date.set\** methods return a number that is the updated time value, so you either do it in two steps: `date.setMonth(...); return date;` or you wrap it: `return new Date(date.setMonth(...))`. – RobG Mar 09 '23 at 04:14
  • @Portal - THAT WORKED!! Post it as the Answer and I'll mark it as the solution! – BigRedEO Mar 09 '23 at 14:03
  • I love how someone was super quick to close my question as a "duplicate" without bothering to read the question where I say that it's in KATALON RECORDER and therefore extremely limited in how javascript can be used. Luckily, I posted @Portal 's answer over in the Katalon forum where people are NICE. – BigRedEO Mar 09 '23 at 17:40
  • 1
    @BigRedEO good looking out, glad it worked – Portal Mar 09 '23 at 18:39
  • Thanks to @RobG for mistakenly closing my question without bothering to read the fact that because it's in Katalon Recorder, traditional javascript doesn't work and thanks to Portal for the unconventional way to find it which works around Katalon Recorder's limitations. This question could help someone else Rob, if you'd read it and re-open it. – BigRedEO Mar 10 '23 at 13:11
  • @BigRedEO—you can vote to reopen questions yourself if you wish. You can also post an answer and accept it. Strictly, the marked duplicate does answer your question, you just needed to use it correctly per @Portal's suggestion. – RobG Mar 11 '23 at 11:50

1 Answers1

2

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

Portal
  • 122
  • 7