Trying to make a date condition based on user input


#1

I have a text field where the user will be inputting the date in the form DD/MM/YYYY. I need a case to trigger only when that date is within 2 months of todays date. Any ideas on how I can do this?

Thanks in advance!


#2

Here is a thread with good information about date calculations


#3

Hi!

If you are in a country where days precede months, e.g., DD/MM/YYYY, you first have to reorder that string to be MM/DD/YYYY, which is a real pain without arrays or regular expressions. This will do it, assuming the variable “d” contains the original date (in DD/MM/YYYY format), and “ms” is the variable that will end up with milliseconds of that original date. (If anyone knows an easier way to get the month portion…)

  set (variable) ms to [[d.substring(d.indexOf('/') + 1, d.lastIndexOf('/')) ]]
  set (variable) ms to [[ms]]/[[ d.substring(0,d.indexOf('/')) ]]
  set (variable) ms to [[ms]]/[[ d.substring(d.lastIndexOf('/') + 1, d.length) ]]
  set (variable) ms to [[Date.parse(ms) ]]
  • The first expression gets what’s between the first slash and the second slash (MM)
  • The second expression takes the string returned by the first expression, adds a slash to the end, and then adds everything before the first slash (MM/DD)
  • The third expression takes the string returned by the second expression, adds a slash to the end, and then adds everything after the last slash (MM/DD/YYYY)
  • The last expression takes the result of the third expression and parses it, returning the milliseconds of that date.

[Edit]. I did indeed find an easier way to get the middle portion (between the slashes). I updated the first expression above.

[Edit] It occurs to me that you can do this with a single, preposterous expression rather than four:

  set ms to [[ Date.parse(d.substring(d.indexOf('/') + 1, d.lastIndexOf('/')).concat('/').concat(d.substring(0,d.indexOf('/'))).concat('/').concat(d.substring(d.lastIndexOf('/') + 1, d.length))) ]]

How to create a date object from Unix Epoch or a given date?