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.

Comments

Any news on this item?

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.

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?

Thanks

Hi, I've added:

<?php
 
// Change date format for timestamp
  // Add Dutch date formats
 
drupal_add_js('sites/all/themes/yourtheme/js/jquery.ui.datepicker-nl.js');
 
// 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.

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

<?php
function mymodule_js_alter(&$js) {
 
$js['settings']['data'][] = array('better_exposed_filters'=> array('bef_dateformat'=>'dd-mm-yy'));
}
?>

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:

<?php
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: http://drupal.org/node/1409120#comment-6198834

It appears there is a patch that fixed exposed filter format for Views Filters: http://drupal.org/node/1409120#comment-6348072 I have not tried it yet. Hopefully it gets added to Date in the next release.

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

Hi,
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 ..

Thanks.

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", "dd.mm.yy." );
  });',
      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.

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:

<?php
/**
* Implements hook_js_alter().
*/
function MODULE_js_alter(&$js) {
 
$js['settings']['data'][] = array('better_exposed_filters'=> array('datepicker_options' => array('dateformat'=>'yy-mm-dd')));
}
?>

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

Created my own module as suggested with exact code:

<?php
function mymodule_js_alter(&$js) {
 
$js['settings']['data'][] = array('better_exposed_filters'=> array('bef_dateformat'=>'dd-mm-yy'));
}
?>

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,
Davide

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

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

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,
Davide

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.

FYI for All...

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

http://www.iso.org/iso/home/standards/iso8601.htm