Set Next Target Weekday for Date Field

This snippet modifies the date to the next specified weekday for a target field.

Instructions

  1. Install our free Custom Javascript for Gravity Forms plugin. Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/
    1. Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin.

Code

Filename: gpld-next-target-weekday.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
 * Gravity Perks // Limit Dates // Set Next Target Weekday for Date Field
 * https://gravitywiz.com/documentation/gravity-forms-limit-dates/
 *
 * Video: https://www.loom.com/share/02b1e6ebd2a74e6aa05742b68fd9322d
 *
 * This snippet modifies the date to the next specified weekday for a target field.
 *
 * Instructions:
 *   1. Install our free Custom Javascript for Gravity Forms plugin.
 *      Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/
 *   2. Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin.
 */
gform.addFilter( 'gpld_modified_date', function( modifiedDate, modifier, date, data, fieldId ) {
	var targetFieldId = 2; // TODO: Change this to your target field ID
	var targetWeekday = 0; // TODO: Change this to your desired weekday (0-6, where 0 is Sunday and 6 is Saturday)

	if ( parseInt( fieldId ) !== targetFieldId ) {
		return modifiedDate;
	}

	if ( ! ( date instanceof Date ) || isNaN( date ) ) {
		return modifiedDate;
	}

	var day             = date.getDay();
	var daysUntilTarget = ( targetWeekday - day + 7 ) % 7;
	if ( daysUntilTarget === 0 ) {
		daysUntilTarget = 7;
	}

	var nextTargetDay = new Date( date );
	nextTargetDay.setDate( date.getDate() + daysUntilTarget );

	return nextTargetDay;
} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.