48 votes
Accepted

Change article date format

So after spending more time than I care to admit looking into this, I've come up with two solutions to this. Solution 1 One way to do it is with the date function that darol100 mentioned in his ...
user avatar
  • 971
41 votes
Accepted

How to get formatted date string from a DateTimeItem object

A date field has two properties, value to store the date in UTC and date, a computed field returning a DrupalDateTime object, on which you can use the methods getTimestamp() or format(): // get unix ...
user avatar
  • 89.2k
32 votes
Accepted

How to show a date in the correct timezone?

I suspect the problem has to do with some of the nuances of strtotime(). strtotime() can take just about any string, and convert it into a Unix timestamp. The problem is that when the string doesn't ...
user avatar
  • 37.7k
23 votes
Accepted

What can I use as default value for a datetime form element?

use Drupal\Core\Datetime\DrupalDateTime; and in form $form['start_date'] = array( '#type' => 'datetime', '#title' => t('Start Date'), '#default_value' => DrupalDateTime::...
user avatar
  • 3,608
19 votes
Accepted

How to print custom date format in Twig?

Try to use this - {{ item.content.field_publication_date|date("M") }} But, I think you will get your solution in {{ item.content|date("M") }} Where item.content contains the date string inside ...
user avatar
18 votes

How to print custom date format in Twig?

First, create the custom date format my_custom_date_format in the configuration section of the drupal admin. Second, use the format_date Twig filter, like here: {{ node.created.value|format_date('...
user avatar
16 votes
Accepted

How to print date in format «long» on node.html.twig template?

you are not using the actual timestamp. Here's what you should do: {% set date = node.createdtime|format_date('long') %} See? You only need to replace created with createdtime. Hope this helps.
user avatar
  • 850
15 votes
Accepted

How can I use 'xx time ago' as date format?

There are two ways to tackle this. One is to just use the field formatters. If you have a DateTime field, just goto Manage Display, and change the Format to Time ago. Then click the gear, and pick ...
user avatar
  • 37.7k
15 votes
Accepted

Inserting the value from Datetime field form

Datetime fields in Drupal 8 are stored as strings, partially because date support between databases isn't consistent and partially to make them easy to manipulate to extract parts where there aren't ...
user avatar
  • 37.7k
14 votes
Accepted

How to filter a View by Date Range (start, end)?

Surprisingly this is not possible with Drupal 8 yet. But there is a long history of trying to make it work: https://www.drupal.org/node/2786577 The last patch, you find in this post, seems to work. ...
user avatar
  • 2,008
13 votes

Date popup in exposed filter

You don't need BEF to get popup calendar with exposed view filters. You just need "Date pop up" and "date views" submodules. When creating new exposed filter in "Filter" dropdown select "Date" then ...
user avatar
13 votes

Trying to render a custom date format with Twig

Here is an approach that uses just Twig (no preprocess functions needed): <p>{{ node.createdtime | date("d F Y") }}</p>
user avatar
  • 5,270
11 votes

Change article date format

If you want to change the date format base on article content type level. You can edit/add the following code post.published_at|date and use a filter to alter the date format. node--article.html.twig {...
user avatar
11 votes
Accepted

How do I get a field value?

Nevermind, this worked for me: $node->get('field_office_hours')->getValue();
user avatar
  • 1,011
11 votes
Accepted

Set timestamp field default value empty

There is backstory to this that is a little complicated. The Timestamp field type was added back into Drupal 8.2.0 core as a "UI Field". The field type had always been there, just as a behind-the-...
user avatar
  • 37.7k
10 votes

Change article date format

I'm more for Chris's solution 2. As he says it IS more the drupal way... but it's not the best option yet. format_date() has been deprecated and will be removed before Drupal 9... meaning that one ...
user avatar
  • 101
10 votes

How do I get the Unix timestamp?

You shouldn't use format_date() anymore. You should create a DrupalDateTime object, and then use the format() method. $date = \Drupal\Core\Datetime\DrupalDateTime::createFromFormat('c', $...
user avatar
  • 37.7k
9 votes
Accepted

basic difference between Date, Date(ISO format), Date(Unix timestamp) to create a field

Below explains three field types in details Date Called a 'datetime' field type in techno database speak. This format type is recomended for most installs because it takes advantage of the database'...
user avatar
  • 1,038
9 votes
Accepted

How to have recurring date events?

As of 2016/03/04, these are open issues in Drupal 8. See Support for end date and D8: Date repeat feature requests. The decision was made to move these from contrib into core, but they are ...
user avatar
  • 37.7k
9 votes
Accepted

How can I get node creation date?

Code for getting node created date. $date = $variables['node']->getCreatedTime(); // Here you can use drupal's format_date() function, or some custom PHP date formatting. $variables['date'] = \...
user avatar
  • 2,091
9 votes

How to configure an exposed filter in a view using a date picker?

You can use the Date Popup module. Adds the native HTML 5 date popup widget to all date fields in views filters. As the Use form element of type date instead textfield when selecting a date in ...
user avatar
  • 5,134
9 votes
Accepted

Set a datetime field configured as date-only

Assuming field_profile_geburtsdatum is configured as a singular date-only datetime field, the following are all equivalent in the Field API (in order of my preferred usage): $user->...
user avatar
  • 37.7k
8 votes

How to print the timestamp of date in a Views fields?

There is a way: Go to Configuration > Regional and language > Date and Time > Formats (Tab) Then click "add format" to add a new date format and just type "U" in the textfield. The "U" is actually ...
user avatar
  • 669
8 votes
Accepted

How to create a chart about the number of node views per day (since last week)?

Using the Charts module Step 1: Create the tabular format of the view First to address the "if it's possible to list last 7 days date in views at all or not" part of your question, you ...
user avatar
8 votes

How to use traditional (national/native) dates and calendars?

I'm hoping that someone else can Optimize this solution and make it better. I'm ready to receive Any suggestion or criticism or any Other solution for this Challenge. Features: Does not requires any ...
user avatar
8 votes

How to show a date in the correct timezone?

Based in Handling timezone conversion with PHP DateTime I modified my code to this: $node = Node::load(2100); $userTimezone = new DateTimeZone(date_default_timezone_get()); $gmtTimezone = new ...
user avatar
8 votes
Accepted

How to get date field object value?

Try $node->get('start_time')->getValue(); This actually gives you an array, if you inspect/dump the array, then you'll see that you can get the value like this. $node->get('start_time')->...
user avatar
  • 29.7k
8 votes
Accepted

My Date in Twig is ignoring format

You are printing the node value directly, which is bypassing the field formatter and other things (since you are not using the render array). Do this instead: {{ content.field_date }} This will ...
user avatar
  • 26.8k
7 votes

how to avoid fieldset in drupal date field

Use the latest full release or the 7.x-2.x-dev version of the Date module. There is an new option Render as a regular field available there: The option was added at the Make it possible to disable ...
user avatar
  • 5,134

Only top scored, non community-wiki answers of a minimum length are eligible