How to customize the Date Picker

Getting a properly formatted date from a user can be a pain.  Did they type with dashes, slashes, spaces, commas, or a combination of everything?

This is where a Date Picker comes in handy.  A Date Picker is a JavaScript tool that pops up a little calendar that lets a user click the date they want and it will format the date in the way you desire.  In Visual Form Builder’s case, the Date Picker is built using the jQuery UI Date Picker.

Default Date Picker Configuration

This Date Picker is a highly complex and customizable jQuery plugin.  So, in order to meet the needs of the many, Visual Form Builder uses the default configuration.  Today, I’m going to explain how to customize the calendar to work like you want it to.

Before we begin, you need to decide what you want to do:

  1. Change the configuration for all Date Pickers on all forms
  2. Change the configuration for each Date Picker on each form (if desired)

In either case, you’ll need to create a JavaScript file and properly add it to your theme via the functions.php file.

Create a JS file and add to your theme via functions.php file

Go into your theme’s folder and create a new file called my-js.js in a folder called js.  Create that folder if it doesn’t exist.

Now, go to your theme’s functions.php file and add the following code:

function my_scripts_method() {
   wp_register_script( 'my-js-file',
       get_template_directory_uri() . '/js/my-js.js',
       array( 'jquery' ),
       '1.0',
       false );

   wp_enqueue_script( 'my-js-file' );
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

You are now ready to configure your JavaScript file.

Configure All Date Pickers on All Forms

This option will change all Date Pickers on any form you happen to create.  This is handy if you like the default configuration but perhaps need to change a few things about it.  For a full list of Date Picker configuration options, please see the documentation.

Open your my-js.js file and add the following code:

jQuery(document).ready(function($) {
    $.datepicker.setDefaults({
        dateFormat: 'yy-mm-dd'
    });
});

Configured Date Picker with global defaults

Configure a Single Date Picker on a Single Form

This option will change a single Date Picker on a single form.  This is useful is you have multiple Date Pickers on a single form and require different configurations for each.

First, you will need to use your browser to View Source.  Find the ID on the Date Picker you want to configure.  Here, I am using Firebug to find that out, but View Source works just as well.

Using Firebug to get the ID

Open your my-js.js file and add the following code:

jQuery(document).ready(function($) {
    $( '#vfb-date-318' ).datepicker({
        numberOfMonths: 2
    });
});

Configured Date Picker for a single field

Bonus: Use Both Configuration Methods

You can use the two methods above to your advantage by combining each technique.  This can be used when you need to set defaults for all Date Pickers yet still be able to configure a single Date Picker on a form.

jQuery(document).ready(function($) {
    $.datepicker.setDefaults({
        minDate: -20,
        dateFormat: 'yy-mm-dd'
    });

    $( '#vfb-date-318' ).datepicker({
        numberOfMonths: 2
    });
});

Configured Date Picker using global defaults with a single field

As you can see, the Date Picker can be complex but it’s also really powerful.


81 thoughts on “How to customize the Date Picker

      1. Nice tip, but I need the datafield without the datapicker for the real birthday. Example: I was born in 1959. If I dont know, how I use the exact dateformat, then I try the datapicker, but the datapicker starts the actual year and I need much time to pick the year 1959. That’s the problem. Therefore I wrote an exampledateformat (13/09/1959) and wont the datapicker off.

  1. Is there a way to turn off the field in which people can enter the date them selves, now people can still enter it as mm-dd-yy, which is tricky.. So only through the picker instead of keyboard?

    Reply ↓
    1. To disable typing into that field, you’d need to make it readonly. This can be done with one line of jQuery.

      Reply ↓
  2. previous and next arrows do not display on the datepicker with jquery..
    if you have any solution so please tell me.

    Reply ↓
  3. Hi! I would like know if it is possible to display the current date as default on the Date Field? Thanks in advance and keep up with the great work! ;)

    Reply ↓
    1. That’s a great question! If you are talking about making the current date the most recent option in the Date Picker, you can insert an option inside the datepicker jQuery. Use minDate:0

      Reply ↓
    1. That error usually means that the jQuery UI JavaScript file isn’t loaded or that you are placing the call to the date picker before the jQuery UI file.

      Reply ↓
      1. Matthew, de datepicker works, however when I try to customize is using you instructions in this post, I also get the “undefined” error.

        You said that maybe ‘you are placing the call to the date picker before the jQuery UI file’.

        Please specify the correct way of doing this.
        Thanks.

      2. Hi, same problem, but I can’t find where to call the UI file Can you help me?

  4. I’m annoyed. I just spent 2 hours doing css and then I find out that with each email confirmation you put your advertisement mark and a link back to your site on the bottom of each email confirmation. Do you think my customers need to see your crap all over my emails like that?

    The least you could do is state up front very clearly that all notifications going back to clients will have your mark and a link back to your site on them.

    Deleted.

    Reply ↓
      1. You’re so right Matthew mailchimp does have their little chimp on their free accounts. Seems to me that a free service takes for granted that there will be a link or ad for you as the creator…paid service = remove the link back if you don’t want it. No need to be so nasty Doug.
        Nice work Matthew – thank you!

    1. This is the best form builder that exist out there. If you are too cheap to spend $10 on a Pro Version that gives you a feature to turn it off, go build your own form from scratch and see how long that will take you.

      No need to take it out on Matthew who has done an incredible job!

      Reply ↓
  5. Nice Plug-in Matthew!

    I recently bought the pro version. Where can I do some further customization of the email confirmation that is sent to the customer? On your “Email Design” page you have an option to set the header background color, but no option to change to the text color. I’d also like to make the size of the header block smaller and perhaps add a logo image.

    I tried hunting around through the CSS files, but I can’t find the the one that controls how the confirmation email is generated. Any help would greatly be appreciated thanks!

    Reply ↓
    1. Hi Jeff,

      The Email Design should take care of most of your needs. Because of the problematic rendering of HTML emails in email clients, it’s necessary for all CSS to be inline. Therefore, there are no CSS files. I’m working on adding a header image upload to the email design as well as the missing header text color option.

      Reply ↓
  6. I’m new to WP and have just tried out this plugin. I’ve managed to get the long date to display on a booking form which I have created, with thanks to this tutorial. My only question is whether the js directory and file and the functions.php will be overwritten when ever there are updates to my theme? Should I be using a child theme? Sorry if these are basic questions, but I am very new to all of this.

    Reply ↓
    1. Great question. Yes, you should be using a child theme to prevent these new files from being overwritten when you update your parent theme.

      Reply ↓
      1. In that case, can you provide some further guidance on how to do this? I can only find info on how to update CSS files in child themes to make customisations. I’m not sure what to do with the js file and the extra code added to functions.php. Thanks in advance.

  7. So I downloaded and installed the pro version, and I was expecting that the date picker was a field that I could just put into my forms without any jquery addition. The only thing I am seeing now is the old date field from the non-pro version that doesn’t have the calendar. Is this what I am supposed to see and do I have to add the jquery myself, or is something wrong?

    Thank You

    Reply ↓
    1. I guess you are talking about being able to select ‘Year to date’ or ‘All Dates Before’? That’s not something that is directly supported by the jQuery UI Datepicker.

      You can try integrating that guys plugin into a regular Text field, though.

      Reply ↓
  8. Is it possible to have a year selection which you would really need for date of birth?

    I love your plugin and I recommended it on Linkedin to the Word press folks. Gary

    Reply ↓
  9. Pingback: Even more Date Picker customizations | Matthew Muro

    1. I don’t believe you can unhighlight today’s date. You can probably use CSS to change the color to match all of the other cells, though.

      Reply ↓
      1. Hi Matthew
        I want to buy your plugin but now i am using your free version but i have two problem right now, one is it cannot sent mail in my gmail, or yahoo mail, and not showing any attachment, so please solve this problem, if it works well then i will buy your plugin.

        Indrajit

      2. First is to make sure that you have the proper permissions set on your wp-content folder. Otherwise, file uploads will not work properly. As far as emails not coming through, make sure you have filled out all of the options in Form Settings > Email. If that’s not working, then you might need to install WP Mail SMTP.

  10. Matthew, for my restaurant reservation form I can only accept dates that are in the future so I would like to disable the ability to choose dates in the past.
    I thought I saw a post about a jquery modification with MinDate:0.
    Can you clarify this a bit since I’m quite new to java scripting…

    Thanks!

    Reply ↓
  11. in reaction to my previous post: found it already Matthew.
    But if you have a moment, can you tell me if it’s possible to disable a day in the picker. My restaurant is closed on mondays and I want to disable this day in the picker…
    thx!

    Reply ↓
      1. Hi Mathew!
        Congratulations to your nice post!
        I am a beginer and using wordpress 3.5.
        I change de alias $ to jQuery. My theme is twenty ten and i am trying using the datepicker in a contact form 7.
        I used the id and putted it on the date field on that contact form.
        Not Work!

  12. Hi Matthew

    Best form creator by a long way.
    Is it possible to set it so that the date format follows the format configured in the WordPress settings? I need ddmmyy, the emails generated always have mmddyy.

    Secondly, it ‘s great to be able to upload files, but I have found it’s possible to upload any file type even though I have listed only doc,, docx and pdf – is this a bug or have I missed something?

    Reply ↓
  13. Hello Matthew, I just purchased your Visual Form Builder Pro today and love your program. We are under a SEVERE time crunch to create, configure and “go live” with 2 “Student Application/Enrollment Forms” prior to Monday morning (National convention and major product launch!) My learning curve is pretty steep but I’m slowly entering all the required questions and form fields. However, I have ZERO css coding experience and I need to ensure I can get both of my new forms 1) looking decent, and 2) functioning properly. I’m in the Atlanta area (Alpharetta) and desperately need some extra help/support. What is difficult for me is probably like “falling off a log” for a guy like you – any help. assistance or recommendations you can offer would be greatly appreciated! Thanks, Rob

    Reply ↓
  14. I have a datepicker where users are not allowed to select future dates.Now the problem is -suppose today’s date is 29th November 2012,so for month drop down december month will not be shown,and for the first time when user selects the year as 2010 or something less than 2012,expected result is, december to be shown in month drop down, but its not showing.
    It is shown if user selects a date in some other month and then goes on to change it again.

    Reply ↓
  15. Hi Mathew,
    I use your function.php code in my functions.php and create js file in theme’s js folder but I didn’t get any date picker in my date’s field, Help me plz.
    Reply me soon.

    Thanks

    Reply ↓
  16. I change another theme it is showing in that theme but I want to use second theme which is not showing date picker is any file conflicting plz help me I am in trouble.

    Thanks

    Reply ↓
  17. hello,

    I changed the date format using this method. It worked. But there is a strange problem. After I uploaded the js folder with js file and edited fuction.php, my wp admin page became inaccessible (white blank page opens.)

    I uploaded the original php and it came back. Then I uploaded edited functions.php, admin page still accessible and after deleting cookies of the browser (chrome) the admin page is again does not open, only a white page opens. Please help.

    Reply ↓
    1. This happens because your PHP tags are not closed properly. Try contacting the theme author to see where the best place in your functions.php file is to place the code.

      Reply ↓
  18. Hi Matthew! Congrats for your plugin!
    A question. Where should i paste the code you give in functions.php?
    I want to change the format to dd/mm/yy.
    I m following your instructions but when i modify the functions.php file seems that the javascript of my theme does not work at all…:(

    Reply ↓
    1. In between the opening and closing PHP tags. I don’t know what theme you are using so you should contact the theme author and ask where it’s best to place the code.

      Reply ↓
  19. Kann mir vielleicht jemand helfen. Mein Englisch ist nicht so gut. Es geht um das Format des Datums. Ich würde es gern anpassen wollen. Derzeit ist es so:
    01/07/2013. Dies möchte ich gern ändern in 7.1.2013 Danke für die Hilfe.

    Maybe someone can help me. My English is not so good. It is about the format of the date. I would like to adapt want. Currently, there is this:
    01/07/2013. This I would like to change in 01/07/2013 Thanks for the help.

    Reply ↓
    1. The instructions for changing the date format are in this very blog post. Look for the section titled “Configure All Date Pickers on All Forms.”

      Reply ↓
      1. Hi Matthew, Thanks for the answer. I use a template of yootheme. As soon as I change something in the function.php, my pictures and Accordions are not displayed correctly. Incidentally, I no longer have access to the support page of yours. You had written to me, but unfortunately I have lost my Order Number. I’m on 13 Mar 2012 09:52:31 GMT paid with PayPal.

      2. Contact the author of the theme and ask where a safe place is to insert the code from the tutorial. It’s typically in between the opening and closing PHP tags.

  20. *Important Note* Many people following this tutorial may find it not working. Somehow the $ symbol for jquery in the script file could be clashing elsewhere. So replace the $ symbol with ‘jQuery’ like in

    jQuery(document).ready(function($) {
    jQuery.datepicker.setDefaults({
    dateFormat: ‘yy-mm-dd’
    });
    });

    Reply ↓
    1. The code I provided is already in noConflict mode and the $ is available as an alias. That’s what the jQuery(document).ready(function($) { line does.

      Reply ↓
  21. Hello Matt,

    I follow all your instruction, it works fine for the first click.
    When I try to revise the date, then it appears this “Please enter a valid date.”

    Do you know why?

    Thank you bro!

    Reply ↓
  22. Hi. This works on pages with a form in them, but it causes problems to my home page, which doesn’t have a form in it.

    The code causes my image slider to not load.

    Renaming /js to /js2 resolves this.

    How do I make the javascript conditional on a form being present?

    Reply ↓
    1. same here – it only works on a site with a form. On all other pages my menu and all jquery elements crash. I dont know how to solve this :-(

      Reply ↓
      1. it helped to move the jquery call to the footer with
        wp_enqueue_script( ‘my-js-file’, ”,”,”,true );

        Not sure if its the perfect solution. For me it only works that way. My function.php is in a child theme by the way…

      2. Sorry I was wrong. Didnt work. Whenever I ad $.datepicker it crashes all pages without a form.

      1. I solved it by moving the script into the footer.php of the template what is not the best choice as this is not good when working with a child theme.

  23. i i can’t configure the data picker il dd-mm-yy and i do no how can change all in my language

    thank’s

    Can i help me ?

    Reply ↓
  24. What if I want a date format that uses the 3 letter abbreviation for the month, instead of numbers? Can I do that too? Do I just use dd-mmm-yyyy?

    Reply ↓
    1. Hi Matthew,

      I’m have difficulties configuring the date picker format. I’ve created the JS file and added it to my theme (Thesis 1.8) via functions.php file. The file paths appear to be correct and I’ve refreshed the browser numerous times but the date remains in the American MM/DD/YY format rather than the standard British DD/MM/YY format which my client needs. Any ideas?

      Also is it possible to format the labels on the address fields. I’d like to change State / Province / Region to the more usual County for the UK.

      Otherwise all good. Very happy and impressed with Visual Form Builder and I’ll be offering it to other clients.

      Best Regards,

      Paul.

      Reply ↓
  25. First off I just bought your program and it looks great. I just need to make a couple of tweaks to the date module and then it should be just what I need. I can’t figure out how to do it though. I want to change the date field to show more than one date: from a date, to a date. The following is what I did, but it doesn’t change the date field? I think the problem is probably in the my-js.js file. Any help or guidance would be greatly appreciated. Thanks

    I put the following in to the function.php file of my theme:
    ——————————————————————–

    //Code for Visual Form Builder
    function my_scripts_method() {
    wp_register_script( ‘my-js-file’,
    get_template_directory_uri() . ‘/js/my-js.js’,
    array( ‘jquery’ ),
    ’1.0′,
    false );

    wp_enqueue_script( ‘my-js-file’ );
    }

    add_action( ‘wp_enqueue_scripts’, ‘my_scripts_method’ );
    }?>
    ———————————————————
    I put this code in the my-js.js file:
    ———————————————————

    jQuery UI Datepicker – Select a Date Range

    $(function() {
    $( “#from” ).datepicker({
    defaultDate: “+1w”,
    changeMonth: true,
    numberOfMonths: 3,
    onClose: function( selectedDate ) {
    $( “#to” ).datepicker( “option”, “minDate”, selectedDate );
    }
    });
    $( “#to” ).datepicker({
    defaultDate: “+1w”,
    changeMonth: true,
    numberOfMonths: 3,
    onClose: function( selectedDate ) {
    $( “#from” ).datepicker( “option”, “maxDate”, selectedDate );
    }
    });
    });

    From

    to

    Reply ↓
  26. Thanks Matthew for your quick response back to me on the datepicker configuration. I have now sort of have things working. I didn’t have the code for the function.php file in the proper place, but now do and able to customize the date picker. I have changed the default to show 3 months of calendars, and that works great. I have the function to have a from and to date: $(function() {
    $( “#from” ).datepicker({
    defaultDate: “+1w”,
    changeMonth: true,
    numberOfMonths: 3,
    onClose: function( selectedDate ) {
    $( “#to” ).datepicker( “option”, “minDate”, selectedDate );
    }
    });
    $( “#to” ).datepicker({
    defaultDate: “+1w”,
    changeMonth: true,
    numberOfMonths: 3,
    onClose: function( selectedDate ) {
    $( “#from” ).datepicker( “option”, “maxDate”, selectedDate );
    }
    });
    });

    All seems good, but where do you put the following?::

    From

    to

    If you could point me in the right direction that would be great. Also how do you customize the form fields? Such as I want to delete the country box from the address and take one of the address boxes away?

    Thanks again, great program, even if I’m somewhat pulling my hair out over this, probably a easy fix.

    Reply ↓
  27. Opps-this is the code I’m wondering where to put for the datepicker to work, seems it didn’t make it into the last post.

    From

    to

    Reply ↓
  28. I am looking to remove access from users to select Saturday and Sunday on the date form (our shop is closed on those days) – how would I go about this?

    Thanks,

    Reply ↓
  29. I have tried using the code you recommend, It seems that I am able to change the date pick, but I have issues with other javascript on the site. When I use your code in the Functions.php file it breaks my Revolution slider plugin and a blog carousel that I have, that also uses Javascript.

    Any help would be great.

    Reply ↓

Leave a Reply

Your email address will not be published. Required fields are marked *