Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.

Dates not behaving as expected

Avatar

Former Community Member

Hello,

I am using LiveCycle Designer 8.05 and I am trying to create a field  where users enter a effective date ("txt_effdate" - a 'Date/Time' field)  and an end date ("txt_enddate" - also a 'Date/Time field) for a  transaction, and if the end date is less than six months from the start  date they get an error message.

I successfully used the code below to read  the "txt_effdate" field and create another field ("sixmos") that  displays a date six months after the start date:

// "sixmos" -  text field

var effDate = util.scand("mm/dd/yyyy",  txt_effdate.formattedValue);
var sFullYear = effDate.getFullYear();
var  sMonth = effDate.getMonth();
var sDate = effDate.getDate();
this.formattedvalue  = util.printd("mm/dd/yyyy", new Date(sFullYear, (sMonth + 6), sDate) );

The trouble  comes in when I try to compare the end date to the "sixmos" date.  The  following code lets all dates pass, meaning it cannot recognize that the  "txt_enddate" is less than the "sixmos" date:

var aeffdate =  util.scand("mm/dd/yyyy", txt_effdate.formattedValue);

if (aeffdate  < sixmos.rawValue) {
xfa.host.messageBox("End Date is less than 6  months from Start Date, please make correction.", "End Date Issue");
                 }

I've  tried comparing "txt_effdate" directly to "sixmos", with various uses  of rawValue and formattedValue extensions.  I've also tried  aeffdate.value instead of just aeffdate... am I missing something simple  here?

Thanks  for the help!

2 Replies

Avatar

Former Community Member

Yes you are missing fundamentals. Comparing dates is not as easy as subtracting one from the other. There are functions provided that you can pass the date to and it will return the number of milliseconds from a point in time (called an epoch). Then when you have both dates in this format you can subtract the two and get a difference (in milliseconds). These functions are much easier to use in FormCalc than Javascript. I have included a couple of samples that I have that use this technique. Start with these and if you need further help do not hesitate to ask.

Paul

Avatar

Former Community Member

Hi Paul,

Thanks for the guidance!  I reviewed the files you attached and they were very helpful... I ended up using a combination of that coding and some of my own and ultimately I achieved my desired functionality.

For the benefit of anyone who finds this post later, here's a portion of my final code:

var aenddate = util.scand("mm/dd/yyyy", txt_enddate.formattedValue);
var aFullYear = aenddate.getFullYear();
var aMonth = aenddate.getMonth();
var aDate = aenddate.getDate();
var theenddate = new Date(aFullYear,aMonth,aDate);

var sixmosdate = util.scand("mm/dd/yyyy", sixmos.formattedValue);
var sFullYear = sixmosdate.getFullYear();
var sMonth = sixmosdate.getMonth();
var sDate = sixmosdate.getDate();
var thesixmosdate = new Date(sFullYear,sMonth,sDate);

if (theenddate.valueOf () > thesixmosdate.valueOf()){
xfa.host.messageBox("The length of the assignment is over 6 months.  Temporary assignments must be 6 months or less.  Please change the Assignment End Date.", "Length of Temporary Assignment Too Long");   
chk_valid.rawValue    = 0;
            }