Title: Datepicker difference &#8211; need zero initial value
Last modified: February 14, 2024

---

# Datepicker difference – need zero initial value

 *  Resolved [acrspf](https://wordpress.org/support/users/acrspf/)
 * (@acrspf)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/datepicker-difference-need-zero-initial-value/)
 * Hi
 * I followed the instructions here [https://wordpress.org/support/topic/forminator-mu-plugins-difference-between-two-dates/](https://wordpress.org/support/topic/forminator-mu-plugins-difference-between-two-dates/)
   for finding the difference in dates, and it works great. 
   Except that the initial
   value returned BEFORE the datepicker fields are used needs to be something I 
   can set manually so the calculations don’t show odd values at first. Is this 
   possible? Can I set an initial value in the mu-plugin code? Thanks everyone 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Support [Jair – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport15/)
 * (@wpmudevsupport15)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/datepicker-difference-need-zero-initial-value/#post-17425345)
 * Hi [@acrspf](https://wordpress.org/support/users/acrspf/),
 * I hope you are doing well today!
 * Yes, you can set an initial value in the mu-plugin code. You can try the following
   code to see if it helps;
 *     ```wp-block-code
       <?php
   
       add_action( 'wp_footer', function(){
           ?>
           <script>
               (function($){
                   $(function(){
                       let _form_id = 1211,
                               _date_field_id1 = 'date-1',
                               _date_field_id2 = 'date-2',
                               _days_field_id = 'number-1',//or enter text-field id
                               _separated_date = '/',
                               _form = $('#forminator-module-'+ _form_id);
                       if( _form.length ){
                           let _date_field1 = _form.find('[name="'+ _date_field_id1 +'"]' ),
                                   _date_field2 = _form.find('[name="'+ _date_field_id2 +'"]' ),
                                   _days_field = _form.find('[name="'+ _days_field_id +'"]');
   
                           // Here, set the initial value for days
                           _days_field.val("YOUR INITAL VALUE");
   
                           _date_field1.on('change', function(){
                               let _value = $(this).val();
                               if( _value ){
                                   _date_field2_value = _date_field2.val();
                                   if( _date_field2_value ){
                                       calc_days( _value.split(_separated_date), _date_field2_value.split(_separated_date), $(this).data('format').split( _separated_date ) );
                                   }
                               }
                           });
   
                           _date_field2.on('change', function(){
                               let _value = $(this).val();
                               if( _value ){
                                   _date_field1_value = _date_field1.val();
                                   if( _date_field1_value ){
                                       calc_days( _date_field1_value.split(_separated_date), _value.split(_separated_date), $(this).data('format').split( _separated_date ) );
                                   }
                               }
                           });
   
   
                           function calc_days( _date_value_1, _date_value_2, _date_format ){
                               let _date1 = {}, _date2 = {};
                               for( let _t in _date_format ){
                                   _date1[ _date_format[_t] ] = _date_value_1[ _t ];
                                   _date2[ _date_format[_t] ] = _date_value_2[ _t ];
                               }
   
                               // Fill age field.
                               _date1 = new Date(_date1['yy'], _date1['mm'], _date1['dd'] );
                               _date2 = new Date(_date2['yy'], _date2['mm'], _date2['dd'] );
                               _form.find('[name="'+ _days_field_id +'"]').val( Math.round( Math.abs( _date2 - _date1 ) / 86400000 ) );
                           }
                       }
               });
           })(window.jQuery);
       </script>
       <?php
       }, 21 );
       ```
   
 * Please replace “YOUR INITAL VALUE” with the initial value you would like. Also
   make sure to update form ID to suit yours. This will set the initial value for
   the days field before any date picker fields are used. Remember that this is 
   a string value, if you want it to be a number you can remove the quotes.
 * Kind regards,
   Zafer
 *  Plugin Support [Amin – WPMU DEV Support](https://wordpress.org/support/users/wpmudev-support2/)
 * (@wpmudev-support2)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/datepicker-difference-need-zero-initial-value/#post-17444127)
 * Hello [@acrspf](https://wordpress.org/support/users/acrspf/) ,
 * It looks like my colleague’s code helped solve your issue as we haven’t heard
   back from you for some time now.
    I will resolve this topic, but if you have 
   more questions feel free to re-open it.
 * kind regards,
    Kasia

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Datepicker difference – need zero initial value’ is closed to new replies.

 * ![](https://ps.w.org/forminator/assets/icon-256x256.gif?rev=3443182)
 * [Forminator Forms – Contact Form, Payment Form & Custom Form Builder](https://wordpress.org/plugins/forminator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/forminator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/forminator/)
 * [Active Topics](https://wordpress.org/support/plugin/forminator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/forminator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/forminator/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [Amin – WPMU DEV Support](https://wordpress.org/support/users/wpmudev-support2/)
 * Last activity: [2 years, 3 months ago](https://wordpress.org/support/topic/datepicker-difference-need-zero-initial-value/#post-17444127)
 * Status: resolved