Copy link to clipboard
Copied
Hey everyone,
I would like to write a script that reveals a field only if an employee puts a particular date range in 2 fields.
Basically, there is Date_Field_1 and Date_Field_2. If an employee puts in a date range between the two which is shorter than 3 days, I'd like a certain field to show up. If they put in a date range between the two which is longer than 3 days, I'd like the field to be hidden.
I can reverse engineer scripts alright, I guess. But I have no idea how to write them on my own from scratch. Can anyone help?
Copy link to clipboard
Copied
Hey everyone,
I would like to write a script that reveals a field only if an employee puts a particular date range in 2 fields.
Basically, there is Date_Field_1 and Date_Field_2. If an employee puts in a date range between the two which is shorter than 3 days, I'd like a certain field to show up. If they put in a date range between the two which is longer than 3 days, I'd like the field to be hidden.
I can reverse engineer scripts alright, I guess. But I have no idea how to write them on my own from scratch. Can anyone help?
Copy link to clipboard
Copied
HI,
Something like the following should help
var curDoc = app.doc
var date1 = curDoc.getField("Date_Field_1");
var date2 = curDoc.getField("Date_Field_2");
// make them into dates, so we can do date maths on them - the field returns a string.
var actualDate1 = new Date ( date1.value);
var actualDate2 = new Date ( date2.value);
// this assumes date 2 is in the future, if it is the other way round you may need to change this line.
var dateChange = actualDate2 - actualDate1;
var millisec3Days = 259200000
// compare to 3 days
if ( dateChange > millisec3Days )
{
curDoc.getField ("Text4").hidden = false;
}
else
{
curDoc.getField ("Text4").hidden = true;
}
Regards
Malcolm
Copy link to clipboard
Copied
Is there any requirement that date 1 must be before date 2?
Do you want the validation to be performed as the user enter the dates into each field or by using a button.?
Copy link to clipboard
Copied
I just made that assumption to keep the code cleaner, the dates could be either way round would just need to code to allow for a possible negative value. if you have a negative value the the "if ( dateChange > millisec3Days )" wouldn't catch the 3 days elapsed days correctly.