Expand my Community achievements bar.

How To Change END Date Based On START Date Picked

Avatar

Level 1

I have two dates, START and END.  When the user picks the START date I want the END date populated + 1 year.  The problem is, when I open the form, the END date is already calculated and posted prior to the START date being picked.  And also, if the START date is changed the END date stays the same. I am using the following JS code in the calculation event;

----- topmostSubform.Body_Page1.BasicInformation.aer_ExceptionDurationScheduleEndDate::calculate - (JavaScript, client) 
// convert date string to a date time object
var oDate = util.scand("mm/dd/yyyy", aer_ExceptionDurationScheduleEndDate.formattedValue);
// extract the full year
var sFullYear = oDate.getFullYear();
// extract zero based month
var sMonth = oDate.getMonth();
// extract date
var sDate = oDate.getDate();
// create formatted display string from month, date, and  year plus 1 year
$.formattedvalue = util.printd("mm/dd/yyyy", new Date((sFullYear + 1), sMonth, sDate) );
3 Replies

Avatar

Level 2

hi aehare70,

i have created a sample form with two date fields,please find the attachment of it.

you can use your same code and place that in start date field exit event by adding " this.rawValue != null " code i.e checking for initial value in start date.

if

( this.rawValue != null)

{

// convert date string to a date time object

var

oDate = util.scand("mm/dd/yyyy",body.sdate.formattedValue);

// extract the full year

var

sFullYear =oDate.getFullYear();

// extract zero based month

var

sMonth =oDate.getMonth();

// extract date

var

sDate =oDate.getDate();

// create formatted display string from month, date, and year plus 1 year

edate.formattedvalue

= util.printd("mm/dd/yyyy", new Date((sFullYear + 1), sMonth,sDate) );

body.edate.rawValue

= edate.formattedvalue;

}

and at the end assign the edate.formattedvalue to Enddate Field.

Thanks,

Madhu.

Avatar

Level 1

Madhu,

Thank you for the reply.  Great answer.  I forgot to ask in my orignal question.. After the end date is posted is it possible to "disable" the end date field so the user can not change the date?

Avatar

Level 2

hi,

after this line

body.edate.rawValue

= edate.formattedvalue;

write

body.edate.access = "readOnly";

to make field readOnly so that user cannot edit.

FYI, to make the field editable use script body.edate.access = "open";

Thanks,

Madhu.