2

We have a content type using a date field, in which the date is entered in the format mm/yyyy. An example entry might be 04/2016. In one particular View, we want to use only the Year value. It appears the token for this field is

[field_delivery_date-value]

In the Rewrite area for this View field, is there an operation that can be performed on the token in order to extract and display only the year value? Or would this need to be done in another way, not using tokens and the rewrite option?

3

Steps to get the just the year for Delivery Date field

  • Navigate to /admin/config/regional/date-time/formats page.
  • Click on Add Format button (/admin/config/regional/date-time/formats/add)
  • In the Format string text field add Y and save it.
  • Now add a date type by navigating to /admin/config/regional/date-time page.
  • Add a new date type and use the the format that we have added in the previous steps.
  • In the view select the type you want the date to be rendered.

enter image description here

So instead of Rewriting the output, you would directly get the year.

Hope that this will help you.

  • Thanks, this is helpful up to a point because I wasn't aware of that option to create a new format. However it doesn't provide the full path to what is needed, and I realize it's probably because the original date entry field is just a text field, not a date picker, in which the user is prompted to enter a date like 04/2016 for April 2016. It looks like that can't be re-rendered with a different date format. Maybe better to replace the original date entry method with a date picker, then this option you suggest would become available? – Charles Apr 19 '16 at 20:38
  • yepp that is recommended if you will be entering the date in it. It would be better if you make that field as a Date field instead of using the text field for entering date. Upvote this answer if you are satisfied with the Answer so that someone else can use this solution in case if they come across this problem. – Nitesh Sethia Apr 20 '16 at 5:24
1

In your view, click on your date field.

Note: In your Content Type your field needs to be a date field, text field won't work!

For formatter, select Custom.

For Date/Time Format, put Y

enter image description here

Note: In Drupal 8 there seems to be a bug, you will have to do this twice, as when you save the 1st time it does not change. In Drupal 7 it should work on the 1st try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.