0

I have a content type with various fields. One of these is a date field. After typing in the date I would like to display the day of the week to the right of the date.

  • 1
    To do this you must create a custom field formatter and get the weekday using this function, api.drupal.org/api/drupal/…. – Jdrupal Sep 9 at 9:18
  • Are you trying to display the day on the admin interface or on the front-end interface? – misterdidi Sep 9 at 9:22
  • Interesting, especially for the personalization of the field. I read the func in drupal.api, but I didn't understand where to put it. the front-end interface – Gaetano Sep 10 at 11:24
1

You can change how dates from date fields are displayed by changing it on the Manage display (/admin/structure/types/manage/[my_CT]/display) page for that Content type.

Custom date format for individual field

The quick and dirty way of doing it is by choosing Custom for Formatter and entering the code for the date, for example:

Y-m-d\TH:i:s, l

will give you "2019-09-09T17:30:19, Monday".

You can lookup the various date and time codes in the PHP manual: https://www.php.net/manual/en/function.date.php

Overriding individual fields displays with custom date formats isn't the best way to go because you can't reuse it throughout the site, for other fields, or translate into other languages (different countries have different conventions about how to write dates).


Reusable Custom date format

The best way is to first add the new Date format to your site. This way you will be able to use anywhere where you can format Date output.

Go to Configuration > Regional and language > Date and time formats and Add a new format.

Name it so it makes sense to your case, enter the PHP date code you want into the Format string, select Language and add the format.

Now visit your Content type's Manage display page and change the Formatter of the Date field you want to affect to Default, and the Date format to the one you have just created.

0

If your question is for showing the day on the content create/edit form, and not displaying after saving that form (which I answered in the previous answer), you'll have to change the widget for the date field.

You can do that through some custom code, but I believe the Bootstrap Datepicker module can do that too (I haven't tried it).

That module has the Date format option where you can enter for example

mm/dd/yyyy DD

to get

09/10/2019 Tuesday

in the Date input field after selecting a date from the calendar popup.

  • Yes thank you, that's exactly what I have to do. I saw the datepicker mosule, but I would like to create a customized code, because then I would have to visualize the form and other information too and at this point instead of installing more modules, the code will be better analyzed. I read something about how to edit custom fields and I also read that "allowed options" could also help, but I saw a partial example. Could you please give me a complete example of both the custom fields and the "allowed options" interface? Thanks – Gaetano Sep 14 at 14:28
  • What do you mean by "allowed options"? If you plan on creating a custom field in a custom module maybe you should open a new question and ask for the code solution specifically. You can probably find questions and answers here that can help, for example: drupal.stackexchange.com/questions/64928/… – prkos Sep 16 at 15:28
  • sorry I was referring to the "allowed values" described in this article. But I would need a complete example to better understand how to interact with drupal thanks – Gaetano Sep 18 at 14:34

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.