Support

Account

Home Forums Backend Issues (wp-admin) Date picker field > Starting from before 1913?

Solved

Date picker field > Starting from before 1913?

  • Hello,

    Do you think it’s possible to set the Date picker range for more than +- 100 years?
    Currently in Date picker I can choose a year between 1913 and 2113. I need the years from lets say 1713.

    I know that the administrator can write the year by hand in the field.

    Thank you.

  • Hi @deian

    If you select a date, the 100 year range should update, also, yes, you can type in the year.

    Perhaps I need to add this in as an option in the backend, however, it’s quite rare that a user would need a larger dropdown that 100 values.

  • Thank you for the answer @elliot.
    We are doing a history website and most of the important people in the 20th century are born before 1913 🙂

  • hi

    would love to see this feature too! Just some way to set the range and start / end years

    hardly a blemish on the great plugin though 🙂

  • If I select a date, the 100 range doesn’t update. I have a similar request/issue regarding the selection of dates. If anyone could shed some light onto this I’d be very grateful.

    http://support.advancedcustomfields.com/forums/topic/restrict-date-range-for-datepicker-edit-screen/

  • @elliot

    Same question for me,
    Like @deian i have to build an history website with biography for people and projects from the beginning of the 20st century

    Is there a solution ?
    Cheers

  • Hi,

    in Acf 5.5.6 (I use the Pro Version), you will need to edit assets/js/acf-input.js file at line 4736 and 4764 and 4953:

    from:

    
                            // create options
    			var args = { 
    				dateFormat:			this.o.date_format,
    				altField:			this.$hidden,
    				altFormat:			'yymmdd',
    				changeYear:			true,
    				yearRange:			"-100:+100", // value to change
    				changeMonth:		true,
    				showButtonPanel:	true,
    				firstDay:			this.o.first_day
    			};

    to:

    
                            // create options
    			var args = { 
    				dateFormat:			this.o.date_format,
    				altField:			this.$hidden,
    				altFormat:			'yymmdd',
    				changeYear:			true,
    				yearRange:			"-200:+100", // value changed!
    				changeMonth:		true,
    				showButtonPanel:	true,
    				firstDay:			this.o.first_day
    			};

    Then you have to minify input.js in input.min.js because this is the file that is read in production; you can use an online tool like https://javascript-minifier.com.

    Finally clear any browser cache to get the new range.

    Or – more simply and cleaner:

    Use the appropriate hook

    functions.php

    
    function my_admin_enqueue_scripts() {
    
    	wp_enqueue_script( 'date-picker-js', get_template_directory_uri() . '/js/custom_date_picker.js', array(), '1.0.0', true );
    
    }
    
    add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');
    

    custom_date_picker.js

    
    // function to merge two Javavascipt objects IE compatible, from prototype.js http://prototypejs.org/doc/latest/language/Object/extend/
    function extend(destination, source) {
    	for (var property in source)
    		destination[property] = source[property];
    	return destination;
    }
    
    // Acf hook -> Hooks -> Filters -> date_picker_args
    // see https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
    acf.add_filter('date_picker_args', function( args, $field ){
    	
    	// do something to args
    	
    	var custom_args = {
    	  yearRange:			"-200:+100", // value to change
    	};
    	
    	args = extend(args, custom_args)
    	
    	// return
    	return args;
    			
    });
    
  • Unfortunately, this solution does not work for dates before 1000. Even if you manually enter a date such as 01/01/0050 (50 AD), it will not save. The resulting behavior is unpredictable, sometimes the field resets to NULL, other times a different date is chosen.

  • This solution (using the admin_enqueue_scripts hook) worked for me – thank you!

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Date picker field > Starting from before 1913?’ is closed to new replies.