I have a calendar view and when displaying in month or day view, I'd like to have the current date displayed in a user friendly manner such as "June 2017".
I created a contextual filter in year_month format which is YYYYMM.
I create a calendar header and of the available options, {{ arguments.field_date_value_year_month }} appears to be the best.
The problem is that I want to format the date in a user friendly format. The problem is that the twig date filter accepts a limited range of strings that it can parse and neither YYYYMM or CCYYMMDD are supported.
For moth view, I was able to work around it by changing the header template to
{{ arguments.field_date_value_year_month|split('', 4)|join('-')|date('F Y') }}
I split the input string into 4 character segments and then join them back together with a dash to convert the date into something like 2017-06. The date filter can parse this.
I haven't figured out full date 20170623 yet since I need to split into 4 chars, 2 chars, 2 chars.
Surely there is a better way to format the date in a calendar header and I'm being quite obtuse.
Comments
Comment #2
jwkovell CreditAttribution: jwkovell commentedIf I understand correctly, you're manually creating your contextual filter instead of using Views templates. Is that correct?
When I create a calendar, the header includes a nicely formatted date (July 2017) by default. If you are not using views templates, perhaps you're missing out on some additional formatting it does in the background? Just a guess.
I can tell you that when I go into header settings, the twig code is just {{ arguments.field_name_value_year_month }}, but it displays in "F Y" format.
Comment #3
bkat CreditAttribution: bkat commentedI had no idea about the views templates. I recreated my calendars from the templates and the date is nicely displayed now.
Comment #4
rokzabukovec CreditAttribution: rokzabukovec at Agiledrop - Your Trusted Drupal Teammates commentedClosing issue since the reporter got his answer.