Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

How can I validate date to be half year later than current date?

Avatar

Former Community Member

Can anybody help me with validating date selected by user with Date picker?

If the Date is later half year from today - it is valid, otherwise invalid. I need to keep focus within the field if invalid input supplied.

Thanks inadvance,

Peter.

3 Replies

Avatar

Former Community Member

Peter,

What exactly is the date criteria? 6 calendar months, 180 days?

Steve

Avatar

Former Community Member

Hey,

The validation succeeds if the date entered/picked in the field is 6 months from today.

Thanks again,

Peter.

Avatar

Former Community Member

In essence time in javascript is stored as a long (just like in java) this means that increasing this number by 1000 increases the date by 1second.

We know that 1 day = 24 hours = 1440 mins = 86400 seconds (*1000 for ms)

So what you need to do is calculate the number of days you have during those months and multiply this by 86400000 and add this to the date.

I would also suggest calculating the max time (date + 6months) up in front, if the user specifies a higher date (userdate - currentdate > date_in_6_months), it's actually invalid and you could just replace the userinput by your calculation. This way the end-user is never able to store a date further in time and your validation is already done. You could also try replacing when user_date < current_date, but thats up to you, i don't know your business-logic.

If possible try to provide this data using XML binding, this way you could do the calculations in java/.net or whatever programming lanague you use. JavaScript is one of the most horrible environments to work with dates. It's just a hint.