Created
June 11, 2024 14:31
-
-
Save vfontjr/69de59f3e8cdd3b2c68b633596b00716 to your computer and use it in GitHub Desktop.
Source Code for https://formidable-masterminds.com/date-math-demo/ form
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
<script> | |
/* install this script in the after fields section on the | |
* form's CustomizeHTML page. | |
*/ | |
jQuery(document).ready(function($) { | |
"use strict"; | |
/** | |
* original code found on StackExchange | |
* addmonths: function used to calculate renewal date | |
* date {dateStr} | |
* months {numeric} | |
* returns new Date object | |
*/ | |
function addMonths(date, months) { | |
var d = date.getDate(); | |
date.setMonth(date.getMonth() + +months); | |
if (date.getDate() != d) { | |
date.setDate(0); | |
} | |
return date; | |
} | |
/* clears renewal date */ | |
function clear_renewal_date_val() { | |
$("#field_dtmthdemo_renewal_date").val(""); | |
} | |
/* Working jQuery */ | |
/* multi-field listener for on change event */ | |
$("#field_dtmthdemo_purchase_date, #field_dtmthdemo_purchase_type, #field_dtmthdemo_subscription_type").on("change", function() { | |
/* if transaction is a subscription, continue processing renewal date calculation */ | |
if ( $("#field_dtmthdemo_purchase_type").val() == "Subscription" ) { | |
/* #field_dtmthdemo_subscription_type is a separate value dropdown | |
* the value is passed as the months parameter to addmonths() | |
*/ | |
if ( $("#field_dtmthdemo_subscription_type").val() !== "" ) { | |
/* the magic */ | |
var renewal_factor = $("#field_dtmthdemo_subscription_type").val(), // separate value dropdown value | |
calc_date = new Date( $("#field_dtmthdemo_purchase_date").val() ), // convert the purchase date value from dateStr to date object | |
renewal_date = addMonths(calc_date, renewal_factor ); // retrieve the renewal date object | |
/* convert the renewal date Date object to a dateStr and write results to renewal date value */ | |
$("#field_dtmthdemo_renewal_date").val($.datepicker.formatDate('mm/dd/yy', renewal_date) ); | |
} else { | |
clear_renewal_date_val(); | |
} | |
} else { | |
clear_renewal_date_val(); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment