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.
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.
Let’s create a Date format that changes the above from Mon, 04/13/2020 – 12:00 to Monday, April 13th, 2020.
- Click Add Format
- For the name, enter Full Date – No Time
- Now we need to consult the PHP Date format documentation over at http://php.net/manual/function.date.php
- 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).
- Enter the above into the Format String field and click Add Format.
- 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.
- Click on the gear on the Date field and select your new format.
- Click Update.
- You’ll see the following on a node that uses that field.
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!