Is there an inverse function to toLocaleDateString? (ie. can I feed in 15-Mar-2022 and have it understood for purposes of math?)

To expand on the title:

Our intranet uses dates in the format of 15-Mar-2022. I’m trying to determine the number of days a given date is from today. I’m working in UI.Vision (*see note at bottom), so there’s a bit of mixed-code here, but this is what I’m trying:

const date1 = new Date(${dateToday}); const date2 = new Date(${line[0]}); const diffTime = Math.abs(date2 - date1); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); return diffDays;

where ${dateToday} and ${line[0]} are both dates (strings) in the format mentioned above.

I get “invalid date”.

I briefly thought I could fix it by doing something like this first:

const string = ${line[0]}; const arr = string.split("-"); return arr

(which UI.Vision would then take on as an array called, for example, dateArray)

And then using ${dateArray[0]} as the Day, ${dateArray[1]} as Month and ${{dateArray[2]} as the Year and then feeding it in as so:

const date = new Date(${dateArray[2]}, ${dateArray[1]}, ${dateArray[0]});

But then the issue is that ${dateArray[1]} is going to be “Mar” when the Date() function is going to expect ‘2’ (0-based index of the numeric months).

So I don’t think I’m getting anywhere.

I suppose I could painstakingly associate each 3-character month with its numeric cousin, but I really don’t want to do that in UI.Vision (it is slow enough without adding an additional 12 lines on each of hundreds of iterations!). I can probably construct it into a javascript 1-liner, which would be ok… but I wondered if there was an easier/better way. Something like the opposite of what toLocaleDateString does. My Google-fu found nothing though.

Thank you.

  • UI-Vision is an RPA extension that I use at work where I have very limited ability to install things. Recommending I use an alternative tool is not an option. UI-Vision utilizes Javascript, but I’m limited to one-liners that return a value. That returned value can be a string or an array of strings.