Setting an "End Date" Variable Based on a "Start Date" Variable + 2 Weeks

Maurice Murphy
Tera Guru

There is a business ask to automatically populate an end date field for an access request as licences for the request are restricted to use in 2 week intervals. Our request form has a start date and an end date, and either we need to:

 

Automatically populate the end date as 2 weeks after the user's selected start date.

 

OR

 

Restrict the user from selecting an end date that is 2 week past the start date.

 

I've tried the following scripts:

 

Automatically setting the start and end date when the form is opened:

function onLoad() {
    //Create a new Glide Date for the current date upon the form being opened.
    // Apply the new Glide Date to a variable named "startDate".
    var startDate = new GlideDate();
    // Set the "endDate" variable to "startDate" plus 14 days.
    var endDate = startDate.addDays(14);
	// Set the field value of start date to the "startDate" glide date.
    g_form.setValue("start_date", startDate);
	// Set the field value of end date to the "endDate" glide date.
    g_form.setValue("end_date", endDate);
}

Automatically setting the end date when the start date changes:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    // Set the startDate variable to a new Glide Date based on the start_date field value.
    var startDate = new GlideDate(g_form.getValue("start_date"));
    // Set the endDate variable to a new Glide Date based on the startDate + 14 days.
    var endDate = startDate.addDays(14);
    // Set the end_date variable on the form to the endDate glide date.
    g_form.setValue("end_date", endDate);

}

I haven't found any success with either, and I also can't quite figure out how to potentially go the route of restricting a user from selecting dates too far out in the future. Any help on what I'm doing wrong?

1 ACCEPTED SOLUTION

swathisarang98
Giga Sage
Giga Sage

Hi @Maurice Murphy ,

 

You can try the below code in onchange client script,

 

 

 

var startDate = new Date(newValue);
    var endDate = new Date();
    var offset = 14;
    endDate.setDate(startDate.getDate() + offset);
    g_form.setValue('end_date', endDate);
    //  alert("start date" + startDate);
    //  alert("end date" + endDate);

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

View solution in original post

5 REPLIES 5

swathisarang98
Giga Sage
Giga Sage

Hi @Maurice Murphy ,

 

You can try the below code in onchange client script,

 

 

 

var startDate = new Date(newValue);
    var endDate = new Date();
    var offset = 14;
    endDate.setDate(startDate.getDate() + offset);
    g_form.setValue('end_date', endDate);
    //  alert("start date" + startDate);
    //  alert("end date" + endDate);

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang