How to Add a Custom Date Format in Drupal 8

How to Add a Custom Date Format in Drupal 8

In Drupal 8, the process of adding a new date format is substantially simpler than in Drupal 7.

In Drupal 7, there were several steps involved.

In Drupal 8, the process is substantially simpler:

  • Go to Configuration > Date and time formats.
  • You’ll see that Drupal 8 ships with around a dozen formats, compared with 3 in Drupal 7.
  • Click the blue “Add format” button:

You can now add a date format directly into the “Format string” field.

You wil need to use the PHP syntax on this page. Drupal will give you a live preview of your date format:

Author

  • Steve Burge

    Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. Steve's work straddles the line between teaching and web development.

    View all posts
0 0 votes
Article Rating
Subscribe
Notify of
guest

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Gilberto Mangones
Gilberto Mangones
7 years ago

Inside field in a content type, how to create a field only month y Year. thanks

Kushal Agrawal
Kushal Agrawal
5 years ago

Same question from my side, if you got your answer please reply

pradeep bhuktar
pradeep bhuktar
4 years ago

You are great

Rod Martin
4 years ago

To create a format for only month and year, head over to Configuration -> Regional & Language -> Date formats.  Click Add Format.  
Jan-2020 would be M-Y
1-2020 would be n-Y
January-2020 would be F-Y
January/2020 would be F/Y
01-2020 would be m-Y

Check out https://www.php.net/manual/en/function.date.php for all the formats you can use.

Once the format is created, go to Structure -> Content Types -> Manage Display for the content type field you want to update.  Click on the gear and select your new format.

I hope that helps.

4
0
Would love your thoughts, please comment.x
()
x