Date and Time Formats in Drupal 8

Date and Time Formats in Drupal 8

One of the questions we see regularly is “How do I change the date and time format in my views and posts?”  The most common issue we see is where you are just trying to display a date, but the time shows up even though you’re not storing it!

In this tutorial, we’ll show you how to set up Date and Time formats in Drupal 8.

You’ll note that out of the box, Drupal 8 doesn’t provide many date formats where the time is not included, so that if you’ve configured a date field to only use the date and not the time, Drupal will still display the time when using one of those formats.  In the illustration below, the date field is set a “Date Only” field type, however, the time is still showing in the display.

date time problem

Since the Date module is now part of core, a lot of the work is done for you.  Go to: Configuration -> Regional and Language -> Date and Time Formats.

date time configuration

Let’s create a Date format that changes the above from Mon, 04/13/2020 – 12:00 to Monday, April 13th, 2020.

  1. Click Add Format
  2. For the name, enter Full Date – No Time
  3. Now we need to consult the PHP Date format documentation over at http://php.net/manual/function.date.php date time php net docs
  4. Scroll through the documentation noting that for the above display you’ll need to enter the following: l, F jS, Y.  The characters you need are on the left and the explanation is on the right. (lower case L, F, lower case J, S and Y).
  5. Enter the above into the Format String field and click Add Format. date time add format
  6. Wherever you need this format to appear, you’ll now update the Display for that field.  For instance, in our illustration above, you would head over to Structure -> Content Types -> Article -> Manage Display.
  7. Click on the gear on the Date field and select your new format.date time change format
  8. Click Update.
  9. You’ll see the following on a node that uses that field. date time fixed

As you can see, with Drupal 8, creating date and time formats for any use case scenario is quick and easy once you know where to look!

Instructor

  • Rod holds two masters degrees and has been training people how to do "things" for over 25 years. Originally from Australia, he grew up in Canada and now resides just outside Cincinnati, Ohio. He has worked in both the non-profit and for-profit worlds, in small companies and large corporations. His extensive open source experience includes WordPress, Joomla, Drupal and Moodle and he really knows how to help you get the most out of the system you chose. Rod plays ice hockey a couple of times a week and rides his Goldwing motorcycle pretty much everywhere he can.

0 0 votes
Blog Rating
Subscribe
Notify of
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Christophe

How can the date format be change on the edit form?

Tylor

I’ve been working with Drupal for 4 years and never knew this was an option. I’ve been banging my head with twig templates to get the date to format properly. I’m pleasantly surprised to see it’s this easy for the UI. If only I had found this earlier! Thanks!

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