Datepicker 1 minus 1 month (instead of + 1 month)

This datepicker filter is essentialy “Datepicker #1 plus 1 month” – I want to make minus 1 month.

I’ve tried swapping the minus sign in for the plus sign on the dateMin line, but that doesn’t work.

Any suggestions?

Hi Leslie. What range of dates should be selectable in your second datepicker? The example you shared takes datepicker 1 and ensures that the second date cannot occur sooner than one month in the future. How will your second datepicker work?

Chris, thanks for responding. I’m not looking to limit what date can be selected with datepicker 2, but rather just trying to give it a default. Here’s the use case - teacher is using the form to submit information about a new bluegrass jamming class. Standard $$ discounts are given and the form asks for the discount deadline. The default should be one month prior to the class start date. So datepicker 1 is the class start date, and datepicker 2 is the discount deadline. One month is the default starting point, but the teacher can pick any date prior to the start of the class.

Does that make sense?

It does work for me when I change the + to -. You also need to update the form ID and field ID. Here is the script I used in an HTML field of the form:

<script>
gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
    if ( formId == 491 && fieldId == 2 ) {
        optionsObj.minDate = 0;
        optionsObj.onClose = function (dateText, inst) {
            dateText = new Date(dateText);
            dateMin = new Date(dateText.getFullYear(), dateText.getMonth() - 1,dateText.getDate());
            jQuery('#input_491_3').datepicker('option', 'minDate', dateMin).datepicker('setDate', dateMin);
        };
    }
    return optionsObj;
});
</script>

In that script, the form ID is 491, the class date (first date) is field 2, and the date one month prior (deposit date default) is field 3. Be sure you update all those numbers.

Thanks, Chris. What you’ve sent confirms what I thought, which is substituting the plus sign with a minus sign would get it done (plus updating the form ID and field number).

Looks like I can only get one snippet to work at a time in the “Custom Javascript” section at the bottom of Form Settings. Do I need any special code to separate them or open/close tags?

In summary, I have (2) different snippets (both datepicker related) and each will work separately, but not when I put them both in the Custom Javascript section. FYI, I’m prepending each with /block comment tags/.

Thanks!

I’m not sure about adding them with block comment tags. I just added the script to the form in an HTML field, with opening and closing tags. You can use multiple snippets at the same time, but without seeing them all, it’s hard to say what is going on in your specific instance.

Chris, I think we can close this out. The script itself does work, and that was the focus of this thread. I’m having trouble getting 2 different scripts to both worth at the same time, and I assume it’s related to how I’m adding it via an HTML field and/or the Custom Javascript option. I will start a new thread. I appreciate your assistance!

OK, thank you.

© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.