Change Date format on Views BEF | Drupal 8

Change Date format on Views BEF

Submitted by editor on Mon, 08/29/2016 - 11:03
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' });

 

Add new comment