I am working on a D8 instance and i would like to have 2 dates which are in different fields to be displayed at once when the month is the same i.e. : 21-22 december instead of 21 december 22 december.
What is the best way to do that ?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.Sign up to join this community
This isn't a direct answer to your question, but is probably the best option to move forward.
Drupal 8.2.0 (due 2016/10/05) will include the new datetime_range module, which provides date ranges in a single field.
I would wait until this is available, and then migrate your two datetime fields into a single daterange fields (one to the start value and the other to the end value).
Then, I would make a custom formatter for daterange fields that implements your logic to combine month/year when they are the same for start and end.
You can then configure your various vide modes to use this new formatter.
Add a hook_preprocess_node function to your theme to check if both dates are in the same month. If yes then the function should hide one date and alter the display of the other to display the range. Here is an example of it.
Alternatively, for views you can use pre_render to alter fields conditionally.