Compare dates in a Case statement

Hi All,

I’m trying to apply some case logic based on dates that have been input by a user. Does anyone know how to do this?

For the date input I am using a simple text field rather than a date field (the date field is somewhat difficult to use).

I have tried turning the input date into a string in ‘yyyymmdd’ format without success. I have been using [[LVAR1.getFullYear()]][[LVAR1.getDay()]][[LVAR1.getMonth()]] - it gives me strange output.

In my case, I want to be able to say “If the [input date] is before [some date] then do [something]”.

Any ideas?

David :slight_smile:

1 Like

Hi David ,
Try to below thread.
Missing Date format for Sort Repeater (Formatting, Formatted Date)
I hope it will be useful for you.

Thanks,
Vikram

Hello Cybertrack,

Do you still need help with this? I’m not sure I’ve understood your question completely. What is the format of the input date?

You could, for example, instruct the user to input a date in the “YYYY.MM.DD” format and then use:

Date.Parse(inputdate) to compare the dates.

Like in this screenshot:


And the file is attached.

But, I’m still not convinced I’ve understood your original query. If I’ve missed the mark and you still need help, I’m happy to take a look!
DateCompare.rp (53.9 KB)

Thanks Tuomas, that is close to what I;m trying to do. I’m expecting that the user will enter the date in dd/mm/yyyy format (Australian standard entry format). I then need to convert this into a number that I can use to compare to other dates in a case statement. Ideally I would convert this into a string like yyyymmdd. This will help me write rules such as ‘if the user is under 18, give them the child form’. If there is a simpler way to do this I’d love to know. :slight_smile:

Is there a way to make date.parse() work with dd/mm/yyyy?

Cheers

Cybertrack,

You can rearrange the input string from dd/mm/yyyy to yyyy.mm.dd so it plays nice with the date.parse() function. I’ve attached another copy of my example with a new page that demonstrates two paths you could take.


It will be a bit easier if you separate each date component into different text fields. If you set the day to LVAR1, month to LVAR2, and year to LVAR3, then you can set a variable (or set text) to:

[[LVAR3]].[[LVAR2]].[[LVAR1]] to rearrange it to yyyy.mm.dd, which you can then parse. You are basically just combining three strings.

If you want to use just one Text Field, you can rearrange it using the substring(x, y) function which will return the characters from x to y. Using a local variable set to the input Text Field, you could use the following function to rearrange the date:

[[LVAR1.substring(6,10)]].[[LVAR1.substring(3,5)]].[[LVAR1.substring(0,2)]]

So [[LVAR1.substring(6,10)]] would return characters from (but not including) the 6th to the 10th character–characters 7 through 10–i.e. the year.

Then you can use the [[Date.Parse()]] function to compare your dates. Just so you know, date.parse() converts the date string into the milliseconds elapsed since January 1, 1970, 00:00:00 UTC.

I also wanted to call you attention to the addyears() function which you can use to add or subtract 21 years for your comparison. So once my date is arranged in the yyyy.mm.dd format, you can add or subtract years using the following function:

[[LVAR1.addYears(21)]]

Where LVAR1 is the rearranged date input.

I hope that makes sense. See the file!
DateCompare.rp (72.7 KB)

Hi Tuomas

I need what you detail in the .rp file that you attach.

But this does not work even in your .rp file

You could correct it and resend it so you can finish understanding your logic.

Thank you very much for your time and help.

Hi Lucho,

I see Tuomas’ file working on my end. Could you let me know which part isn’t working for you? And which browser are you using to preview? Please also make sure you are running the latest build of Axure RP via “Help > Check for Updates” from the main menu. Thanks!