1

Our organization's style guide expects dates in this format:

Oct. 31 2018 01:30 p.m.

I looked the date format drop-down in our views, and there were several options, including a custom one. However, I noticed that none of these are able render the post meridien with abbreviating periods.

There is option for a custom date format, but it seems to use the encoding for PHP date() function. Looking at the manual, there is a, which gives me pm, and A, which gives me PM, but I don't see anything specifying one with periods.

I looked in the code base for where date formatters are stored, and they all seem to be yaml files specifying date() formats:

$ cat core/modules/system/config/install/core.date_format.html_yearless_date.yml
...
pattern: m-d
...

So it seems that the built-in custom date formats are limited to what can be specified with PHP's date() function.

I googled for ways to create a custom date format, but only found explanations of where to specify a custom date format: https://www.ostraining.com/blog/drupal/d8-dates/

How can I create a date formatter where I can use PHP code to give myself a date format with properly abbreviated meridiens?

2

You are correct, the core date.formatter service uses the native PHP date formatting strings, none of which have 'a.m.' or 'p.m.' in them.

If you really need this, I suspect this would work.

  • Create a new module, with a class that extends DateFormatter.
  • In the format method, call the parent method, and then do string replacment to convert 'am' to 'a.m.' and 'pm' to 'p.m.'
  • Replace the date.formatter service with the one you created.

This may have side effects that I'm not thinking of right now.

  • I think I will just do a js to update the format, but thanks : ) – user1359 Oct 23 '18 at 14:12
0

You can create a new dateFormat like Oct. 31 2018 01:30 p.m here is the steps :

  1. From admin/config/regional/date-time/formats/add Create new Format with M d Y h:i A as Format string. enter image description here

  2. Create field of type date: enter image description here

  3. Manage Display and Manage Form display for the field like the following: enter image description here

  4. In the front you will get:

enter image description here

Note:
If you want to add period after month and pm or am Change M d Y h:i A by M. d Y h:i a.

  • 1
    This doesn't handle the need for "a.m." and "p.m." with the period in the middle, 3v4l.org/JYkPR – mpdonadio Oct 23 '18 at 12:27
  • Hello, thanks for responding. However, I am looking to get a fully abbreviated "a.m." and "p.m.". – user1359 Oct 23 '18 at 12:35

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.