-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gw-count-sundays.js
: Added a new snippet.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Gravity Wiz // Gravity Forms // Count Number of Sundays Between Two Dates | ||
* https://gravitywiz.com/path/to/article/ | ||
* | ||
* Use this snippet to count the number of Sundays between a start and end date, including those dates themselves. | ||
* | ||
* Note: This is a JavaScript snippet and there is no server-side validation to ensure that the calculated value | ||
* has not been tampered with prior to submission. | ||
* | ||
* Instructions: | ||
* | ||
* 1. Install this snippet with our free Custom JavaScript plugin. | ||
* https://gravitywiz.com/gravity-forms-custom-javascript/ | ||
* | ||
* 2. Update the required field IDs to match your own by following the inline instructions. | ||
*/ | ||
// Update "1" with the ID of your start Date field. | ||
const startDateField = document.getElementById('input_GFFORMID_1'); | ||
|
||
// Update "2" with the ID of your end Date field. | ||
const endDateField = document.getElementById('input_GFFORMID_2'); | ||
|
||
// Update "3" with the ID of your Sunday count field. | ||
const countField = document.getElementById('input_GFFORMID_3'); | ||
|
||
function calculateSundays() { | ||
const startDate = new Date(startDateField.value); | ||
const endDate = new Date(endDateField.value); | ||
let sundays = 0; | ||
|
||
// Loop through the dates and count Sundays | ||
while (startDate <= endDate) { | ||
if (startDate.getDay() === 0) { // Sunday is represented by 0 in JavaScript's getDay() function | ||
sundays++; | ||
} | ||
startDate.setDate(startDate.getDate() + 1); // Move to the next day | ||
} | ||
|
||
return sundays; | ||
} | ||
function updateSundayCount() { | ||
countField.value = calculateSundays(); | ||
} | ||
|
||
// Update Sunday count each time a date is manually entered. | ||
startDateField.addEventListener('change', updateSundayCount); | ||
endDateField.addEventListener('change', updateSundayCount); | ||
|
||
// Update Sunday count each time a date is selected via the Datepicker. | ||
gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) { | ||
var origOnSelect = optionsObj.onSelect; | ||
optionsObj.onSelect = function( value, dpObject ) { | ||
if ( origOnSelect ) { | ||
origOnSelect(); | ||
} | ||
updateSundayCount() | ||
} | ||
return optionsObj; | ||
} ); |