Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How do I work with dates?

Avatar

Former Community Member

I've tried several formcalc and  javascript methods, but none of them have worked for me so far.  What I  want to do is take the current date and extract the month and year.   Then I will use these to test against other values.  An alternative is  to build some dates and compare them to the current date.  That date can  be changed by the user after the initial display of the form.

In psuedo  code it would look like this:

// currently in the minimum wage field - current event is initialize

//  MonthYear field is initially set to the current date, but can be changed  by the user

// requiring this to happen again.

var curDate = MonthYear;

var prior10Date =  "07/01/2010";

var prior09Date = "07/01/2009";

var minWage = 8.25;

if (curDate < prior10Date) then

     minWage = 8.00;


if  (curDate < prior09Date) then

     minWage = 7.75;

this.rawValue = minWage;

1 Accepted Solution

Avatar

Correct answer by
Level 4

Ahhh sorry, I failed to explain fully.  HelperFunctions is just a script object (right click the form's root node and say "insert script object".  HelperFunctions is just what I named it.  You could name it Jerry if you want)   This is basically like a javascript file (.js) where you can write a whole bunch of functions using just javascript for reference anywhere on the form.  So if you have multiple fields that need a date formatted from 09/01/2010 to September 1, 2010, you can do that using a javascript function that you create.  For example:

so now, anywhere in our form, we can call convertDate(...) and pass in our string date.

Summary:

     A script object is just a collection of helper javascript functions that you can call anywhere in your form by using the syntax:

     You can name the scriptObjectName anything you want, (I chose HelperFunctions), and functionName can be anything meaningful to you (I chose newDate() and setDate(mm,dd,yyyy);

You'll see in the StateMinimumWage field, I call HelperFunctions.newDate() which goes to the HelperFunctions script object and calls the newDate() function returning the current date to me.

View solution in original post

12 Replies

Avatar

Level 4

// currently in the minimum wage field - current event is initialize

//   MonthYear field is initially set to the current date, but can be  changed  by the user

// requiring this to happen again.

minimumWage::initialize(Javascript) function:

Avatar

Former Community Member

Good catch on the array thing.

Syntaxs is OK now, but it still doesn't work.  The value shown in Preview (Designer) of the xdp is 8.4.  I think that is the xml value (verified by changing it to 3.11).

Further info:

I added an alert box and it is not triggered.  I then added one to the enter event and when the preview was displayed, I clicked to enter that field and the alert wasn't displayed.

Avatar

Level 4

can you upload your form to Acrobat.com?  if not, e-mail it to alex.kloft@gmail.com and I can see what's happening

Avatar

Level 4

http://acrobat.com   sign in and you can upload files to that

or you can  PM me your e-mail address and I can send you an invite to share a  workspace with me and upload your form to that

Avatar

Former Community Member

I have got in to abode.com and have a workspace.  Here is the latest version of the non-working script:

app.alert("before workDate");            // testing, this displays
var workDate = new Date();                // this must come from another field (MonthYear) on the form
workDate.setFullYear(2010,07,01);
app.alert("after workDate");            // testing, never hit this point

var prior10Date = new Date();            // 0 based month so 6 = July
prior10Date.setFullYear(2010,06,01);

var prior09Date = new Date();
prior09Date.setFullYear(2009,06,01);

app.alert("before minWage");            // testing
var minWage = 8.25;
app.alert("after minWage");

if (workDate < prior10Date) {
    minWage = 8.00;        // last year amount
    }
if (workDate < prior09Date) {
    minWage = 7.75;        // prior year amount
    }
this.rawValue = minWage;

Avatar

Level 4

My lesson learned:

In order to use new Date(), I had to put it in a script object.  This allowed me to call it without issue (the Date() constructors were why your form was failing).

Best Practice:

I put a display pattern on your StateMinimumWage field...use it on all of your dollar fields, so you have the trailing 0s...decimal fields remove trailing 0s for some reason it seems.

I have sent you the form from my work e-mail.

Hope that helps you!

Alex

Avatar

Former Community Member

What are HelperFunctions?  A google search found 53 hits and I quit looking after the first 10 didn't tell me what it was.

Also, how do I refer to MonthYear in this event?

Avatar

Correct answer by
Level 4

Ahhh sorry, I failed to explain fully.  HelperFunctions is just a script object (right click the form's root node and say "insert script object".  HelperFunctions is just what I named it.  You could name it Jerry if you want)   This is basically like a javascript file (.js) where you can write a whole bunch of functions using just javascript for reference anywhere on the form.  So if you have multiple fields that need a date formatted from 09/01/2010 to September 1, 2010, you can do that using a javascript function that you create.  For example:

so now, anywhere in our form, we can call convertDate(...) and pass in our string date.

Summary:

     A script object is just a collection of helper javascript functions that you can call anywhere in your form by using the syntax:

     You can name the scriptObjectName anything you want, (I chose HelperFunctions), and functionName can be anything meaningful to you (I chose newDate() and setDate(mm,dd,yyyy);

You'll see in the StateMinimumWage field, I call HelperFunctions.newDate() which goes to the HelperFunctions script object and calls the newDate() function returning the current date to me.

Avatar

Level 4

to get MonthYear, change the script I added to StateMinimumWage from:

to

Avatar

Former Community Member

I think I get it.  You can't call javascript functions such as constructors in livecycle events so you have to isolate them to elsewhere.  They can then be called via the function in the "elsewhere" object.

Any other biggies like constructors that can't be used (and don't cause any syntax errors)?

Finally, how do I reference other form fields?

In the case of the 4044 form, I have to get the date from the MonthYear (it's really a full date) field.  And in the next field after the minimum wage I have to refer to two fields on the form to do a calculation (a division in this case).

I started this before I saw your last response.  I'll try that now...

Thanks!

Avatar

Level 4

Yep, you get it.

I really only put Date related stuff in script objects ("elsewhere" objects), but it's also a place you can put validation scripts to check fields for just numbers, just alpha characters, etc.  They are pretty helpful for reducing the amount of repeated code.

Referencing other form fields:

to get the value of a field, you would call: fieldName.rawValue.  rawValue gets you the value that is displayed in the text field.  Using drop-down lists are a bit more confusing because you want to use the change event to get the new text (otherwise it gives you the old text that was selected). 

to do your divisions in Javascript,

this.rawValue = TotalBenefits.rawValue / StateMinimumWage.rawValue;