Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Change Date format on Views BEF

By editor , 29 August, 2016
Question

How to change the date format on views Better exposed form and jQuery date popup ?

There are several methodes.

Change date format Using custom module.

1. Create a module and use he hook hook_form_views_exposed_form_alter(&$form, &$form_state);
Example:

function mymodule_form_views_exposed_form_alter(&$form, &$form_state){
  $date_format = "d-m-Y";
  $date_fields = array(
    'node_changed',
    'field_myfield_eventdate',
  );
  foreach ($date_fields as $field) {
    if (!empty($form[$field])) {
      if (!empty($form[$field]['#date_format'])) {
        $form[$field]['#date_format'] = $date_format;
      }
      if (!empty($form[$field]['min']['#date_format'])) {
        $form[$field]['min']['#date_format'] = $date_format;
      }
      if (!empty($form[$field]['max']['#date_format'])) {
        $form[$field]['max']['#date_format'] = $date_format;
      }
      if (!empty($form[$field]['value']['#date_format'])) {
        $form[$field]['value']['#date_format'] = $date_format;
      }
    }
  }
}

2. Change date popup format by JS/jQuery or by hook_js_alter(&$js)
2.1 By hook_js_alter
function mymodule_js_alter(&$js) {
  $js['settings']['data'][] = array('better_exposed_filters'=> array('bef_dateformat'=>'dd-mm-yy'));
}

2.2 By JS/jQuers : Add following js (How to add a javascript)
$('.bef-datepicker').datepicker({ dateFormat: 'dd-mm-yy' });

 

Tags

  • Drupal 8
  • Code
  • Drupal 7
  • Views

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer