• Resolved nakafi

    (@nakafi)


    Hello.
    Want to calculate the difference between two dates.
    except weekends only work days.

    how to do this ?

    calculation field not working for this.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @nakafi,

    I hope you are keeping well and thank you for reaching out.

    Unfortunately, there isn’t a readily available out-of-the-box solution for this. However, I have contacted our developers to check potential workarounds. We will provide updates here as soon as we receive feedback on this matter.

    Kind Regards,
    Nebu John

    Hi again,

    We haven’t heard from you in a while, we will go ahead and mark this thread as resolved. If you have any additional questions or require further help, please let us know!

    Best regards,
    Laura

    Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @nakafi,

    Our developers worked on a custom workaround, please test this using a dev/staging website at your end.

    <?php
    add_action(
    	'wp_footer',
    	function() {
    		global $post;
    		if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
    			return;
    		}
    		?>
    		<script type="text/javascript">
    			(($,d)=>{
    				if ( window.wpmudev_forminator_dateranger ) {
    					return;
    				}
    				window.wpmudev_forminator_dateranger = {
    					has_loaded 		: false,
    					dp_start 		: $( '.start-date:not(.forminator-hidden) input.forminator-datepicker' ),
    					dp_end 			: $( '.end-date:not(.forminator-hidden) input.forminator-datepicker' ),
    					diff_field 		: $( '.date-diff:not(.forminator-hidden) input' ),
    					init: function() {
    						$(d).ajaxComplete( function( event, xhr, settings ) {
    							const action = wpmudev_forminator_dateranger.get_action( 'action', settings.data );
    							if ( 'forminator_load_form' == action ) {
    								wpmudev_forminator_dateranger.datepickers();
    							}
    						});
    
    						wpmudev_forminator_dateranger.datepickers();
    					},
    					datepickers: function(){
    						if ( wpmudev_forminator_dateranger.has_loaded ) {
    							return;
    						}
    						$( '.start-date:not(.forminator-hidden) input.forminator-datepicker' ).on( 'change', wpmudev_forminator_dateranger.start_date_changed );
    						$( '.end-date:not(.forminator-hidden) input.forminator-datepicker' ).on( 'change', wpmudev_forminator_dateranger.end_date_changed );
    					},
    					start_date_changed : function(){
    						let start_val = $( this ).val(),
    							end_val = $( '.end-date:not(.forminator-hidden) input.forminator-datepicker' ).val(),
    							start_date = new Date( start_val ),
    							end_date = new Date( end_val );
    						if ( start_date.getTime() > end_date.getTime() ) {
    							start_date.setDate( start_date.getDate() + 1 );
    							$( '.end-date:not(.forminator-hidden) input.forminator-datepicker' ).datepicker( "setDate", start_date );
    						}
    						wpmudev_forminator_dateranger.calculate_date_diff();
    					},
    					end_date_changed : function(){
    						let start_val = $( '.start-date:not(.forminator-hidden) input.forminator-datepicker' ).val(),
    							end_val = $( this ).val(),
    							start_date = new Date( start_val ),
    							end_date = new Date( end_val );
    						if ( start_date.getTime() > end_date.getTime() ) {
    							end_date.setDate( end_date.getDate() - 1 );
    							$( '.start-date:not(.forminator-hidden) input.forminator-datepicker' ).datepicker( "setDate", end_date );
    						}
    						wpmudev_forminator_dateranger.calculate_date_diff();
    					},
    					calculate_date_diff : function(){
                            var iWeeks, iDateDiff, iAdjust = 0;
    						let start_val = $( '.start-date:not(.forminator-hidden) input.forminator-datepicker' ).val(),
    							end_val = $( '.end-date:not(.forminator-hidden) input.forminator-datepicker' ).val(),
    							start_date = new Date( start_val ),
    							end_date = new Date( end_val ),
                                iWeekday1 = start_date.getDay(),
                                iWeekday2 = end_date.getDay();
    
                                iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
                                iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
                                if ( ( iWeekday1 > 5 ) && ( iWeekday2 > 5 ) ) iAdjust = 1; // adjustment if both days on weekend
                                iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
                                iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
    
                                iWeeks = Math.floor((end_date.getTime() - start_date.getTime()) / 604800000);
                                if ( iWeekday1 < iWeekday2 ) { //Equal to makes it reduce 5 days
                                    iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
                                } else {
                                    iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
                                }
    
                                iDateDiff -= iAdjust
    
    							var diff = iDateDiff + 1;
    						if ( start_date!='Invalid Date' && end_date!='Invalid Date' ) {
    							if ( diff== 0 ) {
    								
    							} else {
    								$( '.date-diff:not(.forminator-hidden) input' ).val( diff );
    								$( '.date-diff:not(.forminator-hidden) input' ).trigger('change');
    							}
    						}
    					},
    					get_action: function( name, url ) {
    						if ( ! url ) {
    							return '';
    						}
    						name = name.replace(/[\[\]]/g, "\\$&");
    						var regex = new RegExp(name + "(=([^&#]*)|&|#|$)"),
    							results = regex.exec(url);
    						if (!results) return null;
    						if (!results[2]) return '';
    						return decodeURIComponent( results[2].replace(/\+/g, " ") );
    					}
    				};
    				$(d).ready( function(){
    					wpmudev_forminator_dateranger.init();
    				} );
    			})(jQuery,document);
    		</script>
    		<?php
    	}, 9999
    );

    The code could be added as a mu-plugin: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Please make sure to update the form ID in the code and add the custom class “hidden-by-css” under the styling tab of the form.

    I hope that helps. Please feel free to get back to us if you need any further clarification.

    Kind Regards,
    Nebu John

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Calculate date difference’ is closed to new replies.