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 to compare Date in the TextField or DateTime with Date/Time field?

Avatar

Former Community Member

Hi All,

I am facing an issue with respect Date comparinson. I tried to look into entire discussions but failed to get the answer.

My issue is: I wanted to comparet Date/time field value with Date value which is in TextField.

The functionality of my form is:

I have dropdown list which lists the registered customer ids binded to XML Datasource. On selection of the customer id from the dropdown I am displaying customer registration date in the TextField or say Date/Time field as below code in dropdown change event.

------------

RegistrationDetails.PurchaseEntry.txtRegDate.rawValue = regDataNode.CREATION_DATE.value; (this is TextField control)

------------

In form I have an Date/Time field in which use will select the date of item purchase. In the validation part I want to make sure that, the purchase date selected in Date/Time field must be greater than the Customer Registraiton Date. I able to compare if the customer selects the date from two different Date/Time fields. But how to compare the dates which one is in Date/Time field and Date is in TextField?

I have tried using Num2Date and Date2Num with "YYYY-MM-DD"  format to compare but not succeed!

Can you guys help me in this? Thanks in advance

Regards.

9 Replies

Avatar

Level 10

Hi,

You seem to be using the right functions, so they should work.  Another alternative would be using JavaScript and the something like;

util.scand("yyyy-mm-dd"

, TextField1.rawValue)

this will return a JavaScript Date object.

If you could post your code or form then we could have a closer look.

Bruce

Avatar

Former Community Member

Hi,

thanks for reply.

Here is the code which I am using...

-----------

if($.rawValue < Num2Date(RegistrationDetails.PurchaseEntry.txtRegDate.rawValue,"YYYY-MM-DD")) then

       xfa.host.messageBox("Please enter date greater than Registered Date field value.")
endif

-----------

I am validing the above condition in the FormCalc script in the DateTime fields Exit event.

Hope this helps you to look into the issue.

Regards.

Avatar

Level 10

Hi,

Try

if(Date2Num($.rawValue,"YYYY-MM-DD") < Date2Num(RegistrationDetails.PurchaseEntry.txtRegDate.rawValue,"YYYY-MM-DD")) then

       xfa.host.messageBox("Please enter date greater than Registered Date field value.")
endif

Bruce

Avatar

Former Community Member

Hi Bruce,

I tried the given code... but no luck in that..

Even I tired to display the result in the Messagebox but is show the result as 0.

See the code below:

xfa.host.messageBox(Date2Num(RegistrationDetails.PurchaseEntry.txtRegDate.rawValue,"YYYY-MM-DD"))

it returs ' 0 '.

Any other later native solution?

Regards.

Avatar

Level 10

Hi,

I think that would mean that the date isn't in the yyyy-mm-dd format.  Can you also display the xfa.host.messageBox(RegistrationDetails.PurchaseEntry.txtRegDate.rawValue) to confirm.

Attached is my test form.

Regards

Bruce

Avatar

Former Community Member

Hi,

Yes, I am able to display the date which I have assigned to the text box. In the message box it show the value as '31/05/2009', since in this format i am assigining the date in the text box.

Here is the code which I am assigning the value:

--------

RegistrationDetails.PurchaseEntry.txtRegDate.rawValue = regDataNode.CREATION_DATE.value;

---------

Do I need to change the format while assigning value in text box?

You have told that, you have attached your test form, nothing is the reply. Can you repost the file?

Regards.

Avatar

Level 10

Hi,

Sorry about attachment.  I beleive you will either have to change the format of the field to match the YYYY-MM-DD format in your code or change the code to match your DD/MM/YYYY format.

Bruce

Avatar

Former Community Member

Dear Bruce,

Thanks Bruce, I'll check and let you know.

Regards.

Avatar

Former Community Member

Hi Bruce,

Sorry for delayied reply.

I saw your example, it's working fine. but here i have question

In you example, you have set the date in the format of YYYY-MM-DD as with default value in the bind tab. In my case, at run time I am assigning the date value in text box. Do you prefer to format date before in the format of YYYY-MM-DD and assign or need to format at runtime.

How do I format the dates at runtime which we requied?

Regards.