Follow up from #1289370: The JQuery Datepicker does not work, comment #5 posted by ducktape. Not everyone likes Y-m-d formatting so it would be ideal to be able to pick a custom format for both Date API and Drupal's default date fields.


cees.vanegmond’s picture

Any news on this item?

hwasem’s picture

I'd also be interested in hearing news on this item.

I think I have it somewhat working by using a hook_form_alter in a custom module to set the Exposed Form date format there. I set the Views Date field settings to be Popup and the BEF setting to Default Text Field instead of BEF jQuery UI Datepicker. Now the formats match and the form works.

soulston’s picture

Would you mind sharing the form alter that you used. Also, if you are using the BEF setting of Default Text Field instead of BEF jQuery UI Datepicker, how do you actually get the popup functioning?


cees.vanegmond’s picture

Hi, I've added:

  // Change date format for timestamp
  // Add Dutch date formats
  // Change date format
  drupal_add_js('jQuery(document).ready(function () { jQuery(\'#bef-datepicker\').datepicker("option", "dateFormat", "yy-mm-dd" ); });',
    array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)

Can't recal why I required both add's.
This is not working after an AJAX reload.

stred’s picture

Normally it should be possible to configure the date format somewhere but i didn't see where...

for me the correct js is

$('.bef-datepicker').datepicker({ dateFormat: 'dd-mm-yy' });

but it is better to add the date format in the js settings by using hook_js_alter() because the module checks if a date format is defined in the settings

function mymodule_js_alter(&$js) {
  $js['settings']['data'][] = array('better_exposed_filters'=> array('bef_dateformat'=>'dd-mm-yy'));
hwasem’s picture

Sorry for the delay, soulston. I'm just getting back to my d7 project.

I'm not sure if this is correct after reading the last two comments on js modifications. Since I'm not using BEF ui jQuery Datepicker, those weren't an option for me.

I added the following to my custom module to change the date format:

function mymodulename_form_alter(&$form, &$form_state, $form_id) {
    if($form['#id'] == 'views-exposed-form-myviewname-page'){
	//change format of date fields
	$form['date_from']['value']['#date_format'] = 'm/d/Y';
	$form['date_from']['value']['date']['#description'] = 'MM/DD/YYYY';
	$form['date_to']['value']['#date_format'] = 'm/d/Y';
	$form['date_to']['value']['date']['#description'] = 'MM/DD/YYYY';

Since the Views filter date format was pop-up, I think this is what provides the pop-up. I have the BEF format set to default text format.

UPDATE: I got the code above from this thread in Date>Issues:

It appears there is a patch that fixed exposed filter format for Views Filters: I have not tried it yet. Hopefully it gets added to Date in the next release.

tripper54’s picture

Thanks people, hook_js_alter suggestion from #5 worked well for me.

merajhaider’s picture

I want to insert the small datepicker image (on click open the date picker) beside the text field in my BEF date exposed filter form in from and to text field. instead of onclick on text field. How i will do this. need your help ..


dmiric’s picture

I have a strange problem. I'm using BEF datepicker for 4 fields in my exposed filters.

I tried adding #4 and it works good. Only problem is when I switch my view to work with ajax and autosubmit it formats the date only once. Once ajax gets new results date format is reverted back to old state.

I tried doing it with jQuery like this also

drupal_add_js('jQuery(document).ready(function () {  
      jQuery(".bef-datepicker").datepicker("option", "dateFormat", "" );  
      array('type' => 'inline', 'scope' => 'header', 'weight' => 5)

But I get exactly the same result. I would like to add delegate to that function but I really dont know how.

cafuego’s picture

The hook_js_alter() way of doing this in #5 is probably the best one. However, the js settings array that BEF expects is different from the one in the provided code. Here's what works for me:

 * Implements hook_js_alter().
function MODULE_js_alter(&$js) {
  $js['settings']['data'][] = array('better_exposed_filters'=> array('datepicker_options' => array('dateformat'=>'yy-mm-dd')));
Stephen Ollman’s picture

#5 finally worked for me after trying everything else. THANKS!!

Created my own module as suggested with exact code:

function mymodule_js_alter(&$js) {
  $js['settings']['data'][] = array('better_exposed_filters'=> array('bef_dateformat'=>'dd-mm-yy'));
dvende’s picture

Issue summary: View changes

#5 changes the field format as I want, but the search gives me no results (when is should). And when I clear filters I have:

The value input for field is invalid:
The value 12 Sep 2013 does not match the expected format.

The only thing I've done is implement the hook:

function dav_hook_js_alter(&$js) {
$js['settings']['data'][] = array('better_exposed_filters'=> array('bef_dateformat'=>'dd M yy'));

Am I miss something?

Many thanks,

cafuego’s picture

If your module is called dav, your function name should be dav_js_alter().

dvende’s picture

Thanks for your answer. But no, my module is dav_hook. However the js_alter works: if I click on a day in the datepicker I see my date format in the exposed filter, 12 Sep 2013, instead of 2013-09-12. But 12 Sep 2013 still returns no data, and 2013-09-12 does...

This will work when:

1. the datepicker returns my custom date format
2. apply the filter shows me correct data (the nodes with date 12 Sep 2013)
3. I have no errors as The value input for field is invalid:
The value 12 Sep 2013 does not match the expected format

dvende’s picture

Just an update. I'm a little bit closer to solution. Add in my custom hook module (dav_hook) this:

function dav_hook_js_alter(&$js) {
$js['settings']['data'][] = array('better_exposed_filters'=> array('bef_dateformat'=>'d-m-y'));

function dav_hook_form_views_exposed_form_alter(&$form, $form_state) {
$form['field_concert_date_value']['value']['#date_format'] = 'd-m-y';

The first one to let the datepicker to return "d-m-y" date format, the second one to format the exposed filter field in the same way. Thanks to this I have no invalid format errors, and the filter now retrieves correct data. The "but" is... I would like to use "dd M yy" format (12 Sep 2014), instead of "d-m-y". After few test the problem I suppose the problem is the blank space... "d-M-y" works, "d m y" or "d M y" not.

Any idea is welcome :D
Many thanks,

dvende’s picture

Ok, now it works. After I've found a comment to a PHP post... gotcha. Double quotes instead of single:

function dav_hook_js_alter(&$js) {
$js['settings']['data'][] = array('better_exposed_filters'=> array('bef_dateformat'=>"d M Y"));

function dav_hook_form_views_exposed_form_alter(&$form, $form_state) {
$form['field_concert_date_value']['value']['#date_format'] = "d M Y";

In this way I can have 12 Sep 2014 as date, it works, finds results, no validation errors and finally with double quotes I can use 4 digits year (with single quotes it raises an error).

I hope this can help someone. I'm using Drupal 7.23, Views 3.7, Date 2.6 and BEF 3.0beta3.

fgjohnson’s picture

FYI for All...

We should really be getting used to YYYY-MM-DD. I wish all my international business correspondence used the same format!

keopx’s picture

Version: 7.x-3.0-beta1 » 7.x-3.0-beta4

I solved using this:

function mymodule_js_alter(&$js) {
  $js['settings']['data'][] = array(
    'better_exposed_filters' => array(
      'datepicker_options' => array(
        'dateformat' => 'dd-mm-yy',
W.M.’s picture

#4 solved the issue at my site.

mcartmel’s picture

#18 works for me but not after AJAX (it goes back to the wrong format). Any ideas?

W.M.’s picture

@mcartmel. Change the format within the Better Exposed Filters file line 991 or 1120, I simply cannot remember which since I have lost that hack due to BEF manual upgrade, simply write the desired format (in PHP style I think). That solves the issue.

Of course you can comment out the code there and move that block out to a custom module, so you will have a backup in case you need to update BEF.

MattWithoos’s picture

@W.M. and all other contrib module hackers in this thread...

I'd suggest making a git patch out of your changes. Storing it in the module directory itself means it usually won't be lost with an update - and gives you the flexibility to store it elsewhere too. See here:

Additionally, if you've applied a hack that extends the functionality in a way that improves the experience for others and doesn't change behaviour undesirably for existing users (i.e. in this particular case, the ideal hack would be adding a field in BEF options that users can tick/untick to change the date format), then you can actually upload that patch right here and see it committed.

If I find some time, I'll offer a patch.

mikeker’s picture

#1718292: Allow datepicker options to be overriden added the ability to override the default datepicker settings. You can add

dateFormat: "dd-mm-yy"

to change the date format used by the picker. It's a translatable field so you can set different formats for different languages. Can you let me know if that meets your needs?

mikeker’s picture

Status: Active » Postponed (maintainer needs more info)
mikeker’s picture

Assigned: mikeker » Unassigned
MattWithoos’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)

Oh, nice one!! Can't wait to update the module and start using this. Thanks for your work.

Closing on behalf of the person who posted this 2 years ago, as it clearly fixes their, and all subsequent people's, issue(s).